From 586094916a20b006ef35e394b3a957a22057123d Mon Sep 17 00:00:00 2001 From: Karl Berry Date: Fri, 28 Jan 2022 22:24:21 +0000 Subject: proof-at-the-end (28jan22) git-svn-id: svn://tug.org/texlive/trunk@61776 c570f23f-e606-0410-a88d-b1316a301751 --- .../latex/proof-at-the-end/proof-at-the-end.sty | 169 ++++++++++++++++++--- 1 file changed, 149 insertions(+), 20 deletions(-) (limited to 'Master/texmf-dist/tex/latex/proof-at-the-end') diff --git a/Master/texmf-dist/tex/latex/proof-at-the-end/proof-at-the-end.sty b/Master/texmf-dist/tex/latex/proof-at-the-end/proof-at-the-end.sty index 66a27077b5e..0fad63e1883 100644 --- a/Master/texmf-dist/tex/latex/proof-at-the-end/proof-at-the-end.sty +++ b/Master/texmf-dist/tex/latex/proof-at-the-end/proof-at-the-end.sty @@ -7,7 +7,7 @@ %% proof-at-the-end.dtx (with options: `proof-at-the-end.sty,package') %% %% proof-at-the-end.dtx -%% Copyright 2019 Léo Colisson +%% Copyright 2022 Léo Colisson %% %% This work may be distributed and/or modified under the %% conditions of the LaTeX Project Public License, either version 1.3 @@ -39,16 +39,17 @@ %% Grave accent \` Left brace \{ Vertical bar \| %% Right brace \} Tilde \~} \NeedsTeXFormat{LaTeX2e} -\ProvidesPackage{proof-at-the-end}[2019/05/21 A package to move proofs in appendix] +\ProvidesPackage{proof-at-the-end}[2022/01/28 A package to move proofs in appendix] \RequirePackage{etoolbox} -\RequirePackage{hyperref} \RequirePackage{thmtools} \RequirePackage{thm-restate} \RequirePackage{catchfile} \RequirePackage{pgfkeys} \RequirePackage{xparse} - +\AtEndPreamble{% + \RequirePackage{hyperref} +}% \RequirePackage{kvoptions} %% https://tex.stackexchange.com/questions/109747/put-all-package-options-into-one-command @@ -57,23 +58,52 @@ %% \usepackage[conf={normal}]{proof-at-the-end} \SetupKeyvalOptions{ family=pratendOpt, - prefix=pratendOpt, + prefix=pratendOpt, % "Namespace": Prefix of the commands, conditions.... } \DeclareStringOption{conf} \DeclareLocalOptions{conf}% \pratendOptdefconf contains the proof +\DeclareBoolOption{disablePatchSection} % Use \ifpratendOptdisablePatchSection ... \else ... \fi +\DeclareStringOption[autoref]{commandRef} % Use commandRef=cref to use cleveref. Defaults to autoref. +\DeclareBoolOption{createShortEnv} % To create thmE, theoremE, proofE, lemmaE, corrolaryE \ProcessKeyvalOptions* +%% https://tex.stackexchange.com/a/13289/116348 \newwrite\appendwrite +\newcount\pratend@count@makeallother + +%%% Loop to make sure all chars are normal letters, including #. +\newcommand{\makeallother}{% + \pratend@count@makeallother0\relax + \loop\ifnum\pratend@count@makeallother<255\relax + \catcode\pratend@count@makeallother12\relax + \advance\pratend@count@makeallother by 1\relax + \repeat +} +%%% This command allows the user to append things to a file. But the content may contain macros that +%%% will be evaluated before writing them to the file. +%%% TODO: Efficiency may be improved by writing to the file only before the printProof. \NewDocumentCommand\appendtofile{m+m}{% \begingroup - \IfFileExists{#1}% - {\CatchFileDef{\filecontent}{#1}{\catcode`\\=12 \endlinechar=`^^J\catcode\endlinechar=12\relax}}% keep existing end-of-lines - {\let\filecontent\empty}% - \immediate\openout\appendwrite=#1\relax - \immediate\write\appendwrite{\detokenize\expandafter{\filecontent}#2}% - \immediate\closeout\appendwrite + %% Read the content + \begingroup% + \IfFileExists{#1}{% + \newlinechar\endlinechar% + \makeallother% Turn all chars into normal letters. + \everyeof{\noexpand}% + \edef\fileContent{\@@input #1 }% + }{% + \let\fileContent\empty + }% + %% Open the file to write in it: + \immediate\openout\appendwrite #1\relax% + %% Write the text + \immediate\write\appendwrite{\fileContent}% + \endgroup% + \immediate\write\appendwrite{#2}% + %% Close the file + \immediate\closeout\appendwrite% \endgroup } @@ -189,19 +219,19 @@ one big link={#1~\pageref*{proof:prAtEnd\pratendcountercurrent}.} }, text link section/.style={% - text link={See \hyperref[proof:prAtEnd\pratendcountercurrent]{proof} in \autoref{proofsection:prAtEnd\pratendcountercurrent}.} + text link={See \hyperref[proof:prAtEnd\pratendcountercurrent]{proof} in \pratendSectionlikeCref.} }, text link section full proof/.style={% - text link={See \hyperref[proof:prAtEnd\pratendcountercurrent]{full proof} in \autoref{proofsection:prAtEnd\pratendcountercurrent}.} + text link={See \hyperref[proof:prAtEnd\pratendcountercurrent]{full proof} in \pratendSectionlikeCref.} }, default text link/.style={ text link={See \hyperref[proof:prAtEnd\pratendcountercurrent]{proof} on page~\pageref{proof:prAtEnd\pratendcountercurrent}.}, }, text proof translated/.style={ - text proof={#1 \string\autoref{thm:prAtEnd\pratendcountercurrent}}, + text proof={#1 \string\pratendRef{thm:prAtEnd\pratendcountercurrent}}, }, default text proof/.style={ - text proof={Proof of \string\autoref{thm:prAtEnd\pratendcountercurrent}}, + text proof={Proof of \string\pratendRef{thm:prAtEnd\pratendcountercurrent}}, }, %%%% Defaults bare defaults/.style={ @@ -293,9 +323,13 @@ \fi% %% Create a custom alias to restate the theorem \expandafter\protected@xdef\csname \pratendcustomrestate\endcsname{\noexpand\csname prAtEndRestate\pratendcountercurrent\endcsname}% - %% Restate the theorem if needed in appendix - \ifrestatethm - \appendtofile{\prefixPrAtEndFiles\category.tex}{\string\prAtEndRestate\pratendcountercurrent*} + %% Restate the theorem if needed in appendix. + %% As explained in https://github.com/leo-colisson/proof-at-the-end/issues/8 + %% No need to restate of there is no proof at the end. + \ifproofend % + \ifrestatethm % + \appendtofile{\prefixPrAtEndFiles\category.tex}{\string\prAtEndRestate\pratendcountercurrent*}% + \fi% \fi% \fi% %% If the theorem is not stated in the main text, @@ -306,7 +340,7 @@ \protected@xdef\pratendcountercurrent{\roman{counterAllProofEnd}} %% Create a custom alias to restate the theorem \expandafter\protected@xdef\csname \pratendcustomrestate\endcsname{\noexpand\csname prAtEndRestate\pratendcountercurrent\endcsname}% - \appendtofile{\prefixPrAtEndFiles\category.tex}{\string\begin{restatable}[#3]{#2}{prAtEndRestate\pratendcountercurrent}\string\label{thm:prAtEnd\pratendcountercurrent}\detokenize{#4}\string\end{restatable}}% + \appendtofile{\prefixPrAtEndFiles\category.tex}{\string\begin{restatable}[\detokenize{#3}]{#2}{prAtEndRestate\pratendcountercurrent}\string\label{thm:prAtEnd\pratendcountercurrent}\detokenize{#4}\string\end{restatable}}% \fi% }{} @@ -332,7 +366,13 @@ \fi% %% Write the proof at the end \ifproofend% - \appendtofile{\prefixPrAtEndFiles\category.tex}{\string\label{proofsection:prAtEnd\pratendcountercurrent}\string\begin{proof}[\pratendtextproof]\string\phantomsection\string\label{proof:prAtEnd\pratendcountercurrent}\detokenize{#2}\string\end{proof}}% + %% The label alone adds some space, see e.g. + %% https://tex.stackexchange.com/questions/600118/selectlanguage-in-the-middle-of-text-causes-extra-vertical-space/600142#600142 + %% and https://tex.stackexchange.com/questions/600577/adding-a-label-between-two-proofs-adds-unwanted-space + %% The solution is to use \\Hy@SaveLastskip and \Hy@RestoreLastskip from hyperref + %% See also + %% https://tex.stackexchange.com/questions/553394/reliably-add-a-label-to-the-current-section-even-if-the-label-is-not-placed-rig/600847#600847 + \appendtofile{\prefixPrAtEndFiles\category.tex}{\string\makeatletter\string\Hy@SaveLastskip\string\pratendChangeCurrentLabel\string\label{proofsection:prAtEnd\pratendcountercurrent}\string\Hy@RestoreLastskip\string\makeatother\string\begin{proof}[\pratendtextproof]\string\phantomsection\string\label{proof:prAtEnd\pratendcountercurrent}\detokenize{#2}\string\end{proof}}% \fi% }{} @@ -365,6 +405,95 @@ %%% You can easily modify the defaults: %%% Or create new styles to apply: +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%% Patch sections, chapters... +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +\newcounter{pratend@counter@sectionlike} +%% Command to add after a label like "prAtEnd:sectionlike10". Useful to patch \section, \chapter... +\NewDocumentCommand\pratendAddLabel{}{% + %% First, we get the label + %% Cool explaination of expandafter, edef, noexpand: + %%% https://www.tug.org/TUGboat/tb09-1/tb20bechtolsheim.pdf + \edef\pratend@current@sectionlike@label{prAtEnd:sectionlike\thepratend@counter@sectionlike}% + \expandafter\label{\pratend@current@sectionlike@label}% + \let\pratend@sectionlike@stored@label\@currentlabel% +}% +\NewDocumentCommand\pratendSectionlikeCref{}{% + \pratendRef{proofsection:prAtEnd\pratendcountercurrent}% +} +\NewDocumentCommand\pratendChangeCurrentLabel{}{ + \ifdefined\pratend@sectionlike@stored@label% + \let\@currentlabel\pratend@sectionlike@stored@label% + \fi% +} +\ifpratendOptdisablePatchSection +\else + %% Add the above command to \chapter if the command exists + \ifdef{\@chapter}{ + \ifpatchable*{\@chapter}{ + \apptocmd{\@chapter}{\pratendAddLabel}{}{}% + }{\PackageWarning{proof-at-the-end}{Chapters are not patchable.}{}} + }{} + %% patch sections/subsections/paragraph/... + %% https://tex.stackexchange.com/questions/631713/patch-section-command-fails/631717?noredirect=1#comment1575337_631717 + \ifdef{\@sect}{ + % \apptocmd{\section}{\pratendAddLabel}{}{}% + \ifpatchable*{\@sect}{ + \apptocmd{\@sect}{\pratendAddLabel}{}{}% + }{\PackageWarning{proof-at-the-end}{Sections are not patchable.}{}} + }{} +\fi + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%% Allow cref/autoref/... +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% Instead of \autoref{proof:blabla} use \pratendRef{proof:blabla}, it allows the ref command to be customized +%% as an option of the package. +\NewDocumentCommand\pratendRef{m}{% + \csname \pratendOptcommandRef\endcsname{#1} +} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%% Create automatically wrapper +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +%% Defines a shortcut to avoid typing the whole theoremEnd environment. +%% Avoid to manually type \NewDocumentEnvironment... +%% Usage: \newEndThm[default options]{new environment}{old environment} +\NewDocumentCommand{\newEndThm}{O{}mm}{% + % \expandafter\NewDocumentEnvironment{#2}{O{}O{}+b}{% + % \begin{theoremEnd}[#1,##2]{#3}[##1]% + % ##3% + % \end{theoremEnd}% + % }{} + \NewDocumentEnvironment{#2}{O{}O{}}{% + \begin{theoremEnd}[#1,##2]{#3}[##1]% + }{\end{theoremEnd}} +} + + +%% Avoid to manually type \NewDocumentEnvironment... +%% Usage: \newEndProof[default options]{new proof environment}{old proof environment} +\NewDocumentCommand{\newEndProof}{O{}m}{% + \NewDocumentEnvironment{#2}{O{}+b}{% + \begin{proofEnd}[#1,##1]% + ##2% + \end{proofEnd}% + }{} +} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%% Create even more automatically wrapper with option "createShortEnv" +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +\ifpratendOptcreateShortEnv + %%% We create automatically the environments. + \newEndProof{proofE} + \newEndThm{thmE}{thm} + \newEndThm{lemmaE}{lemmaE} + \newEndThm{theoremE}{theorem} + \newEndThm{corollaryE}{corollary} +\fi + \endinput %% %% End of file `proof-at-the-end.sty'. -- cgit v1.2.3