Closed fpantigny closed 8 months ago
As far as I know, when the key
lowerbox=ignored
is used, the content of the so-called\lowerbox
is composed in a box, even if that box is not used.
Could you show a concrete and complete example? Because savelowerto=<file>
just writes the lower part to <file>
without composing it in a box.
Example below shows that the lower part saved by savelowerto
can be input and then typeset in different ways.
\documentclass{article}
\usepackage{lipsum}
\usepackage{tcolorbox}
\begin{document}
\begin{tcolorbox}[savelowerto=\jobname-lower.tex, lowerbox=ignored]
upper $a + b = c$ \par
\lipsum[1][1-3]
\tcblower
lower $x + y = z$ \par
\lipsum[1][4-6]
\end{tcolorbox}
\begin{minipage}{5cm}
\input{\jobname-lower.tex}
\end{minipage}
\end{document}
Here is an example.
\documentclass{article}
\usepackage{lipsum}
\usepackage{tcolorbox}
\newcounter{mycounter}
\begin{document}
\begin{tcolorbox}[savelowerto=\jobname-lower.tex, lowerbox=ignored]
upper $a + b = c$ \par
\lipsum[1][1-3]
\tcblower
lower $x + y = z$ \par
\lipsum[1][4-6]
\stepcounter{mycounter}
\end{tcolorbox}
\begin{minipage}{5cm}
\input{\jobname-lower.tex}
\end{minipage}
\bigskip
Value of \verb|mycounter|: \arabic{mycounter}
\end{document}
The LaTeX counter mycounter
has been incremented twice.
It is true that the lower part is always composed into a box which is either
invisible
)ignored
)So, counters are incremented as in your example. Boxes with fit
option are even worse since their content is typeset multiple times.
In general, it would be difficult and error-prone to change this behaviour which is part of tcolorbox
since its beginning.
For the special case of savelowerto
, there is a possible patch, since the lower part is saved and loaded afterwards:
\documentclass{article}
\usepackage{lipsum}
\usepackage{tcolorbox}
\makeatletter
\def\tcb@endlowerverbatimanddraw{%
\endtcbverbatimwrite%
\endgroup%
\iftcb@lowerignored\else%
\IfFileExists{\kvtcb@savelowerto}{%
\tcb@savelowerbox\input{\kvtcb@savelowerto}\end{tcb@savebox}}{}%
\fi%
\tcb@draw@color@box%
\tcb@finalize@environment%
}
\makeatother
\newcounter{mycounter}
\begin{document}
\begin{tcolorbox}[
savelowerto=\jobname-lower.tex,
lowerbox=ignored,
]
upper $a + b = c$ \par
\lipsum[1][1-3]
\tcblower
lower $x + y = z$ \par
\lipsum[1][4-6]
\stepcounter{mycounter}
\end{tcolorbox}
\begin{minipage}{5cm}
\input{\jobname-lower.tex}
\end{minipage}
\bigskip
Value of \verb|mycounter|: \arabic{mycounter}
\end{document}
The drawback of this patch is that it has a dependency to savelowerto
. If savelowerto
is used, the counter is not increased, but otherwise it is. So, I am not sure, if this is really a good patch...
The drawback of this patch is that it has a dependency to
savelowerto
. Ifsavelowerto
is used, the counter is not increased, but otherwise it is. So, I am not sure, if this is really a good patch...
Introducing a new choice value like lowerbox=totally ignored
seems not solve the problem nicely, then how about a new key like /tcb/savelowerto and ignored=<file name>
(I'm not good at names)?
\documentclass{article}
\usepackage{tcolorbox}
\usepackage{xpatch}
\makeatletter
\newif\iftcb@lowertotallyignored
\tcbset{
savelowerto and ignored/.code={%
\tcb@lowertotallyignoredtrue
\pgfkeysalso{savelowerto={#1}, lowerbox=ignored}%
}
}
\xpatchcmd\tcb@endlowerverbatimanddraw
{\tcb@savelowerbox\input{\kvtcb@savelowerto}\end{tcb@savebox}}
{%
\iftcb@lowertotallyignored
\else
\tcb@savelowerbox\input{\kvtcb@savelowerto}\end{tcb@savebox}%
\fi
}
{}{\PatchFailed}
\makeatother
\newcounter{mycounter}
\begin{document}
\def\printCounter{\par\bigskip
Value of \texttt{mycounter}: \arabic{mycounter}\par}
\printCounter
\begin{tcolorbox}[savelowerto and ignored=\jobname-lower.tex]
upper $a + b = c$ \par
\tcblower
lower $x + y = z$ \par
\stepcounter{mycounter}
\end{tcolorbox}
\printCounter
\begin{minipage}{5cm}
\input{\jobname-lower.tex}
\end{minipage}
\printCounter
\end{document}
Introducing a new choice value like
lowerbox=totally ignored
seems not solve the problem nicely, then how about a new key like/tcb/savelowerto and ignored=<file name>
(I'm not good at names)?
Yes, this is a possibilty. Since savelowerto and ignore
is a bit more than the combination of savelowerto
and lowerbox=ignored
, I looking for another wording, but, maybe, documenting is enough.
Other possibilities that I can think of:
savelowerto and totally ignore
(clearly(?) but a bit lengthy)solely savelowerto
or savelowerto solely
(the latter is closer to existing savelowerto
but grammatically wrong(?)) A suggestion : redirectlowerto
A suggestion :
redirectlowerto
I think this could be a good name. The following code seems to work:
\documentclass{article}
\usepackage{lipsum}
\usepackage{tcolorbox}
\makeatletter
\newif\iftcb@redirectlowerto
\tcbset{
savelowerto/.code={\tcb@redirectlowertofalse\def\kvtcb@savelowerto{#1}},
redirectlowerto/.code={\tcb@lowerignoredtrue\tcb@lowervisiblefalse\tcb@redirectlowertotrue\def\kvtcb@savelowerto{#1}},
}
\def\tcb@endlowerverbatimanddraw{%
\endtcbverbatimwrite%
\endgroup%
\iftcb@redirectlowerto\else%
\IfFileExists{\kvtcb@savelowerto}{%
\tcb@savelowerbox\input{\kvtcb@savelowerto}\end{tcb@savebox}}{}%
\fi%
\tcb@draw@color@box%
\tcb@finalize@environment%
}
\makeatother
\newcounter{mycounter}
\begin{document}
\begin{tcolorbox}[
%savelowerto=\jobname-lower.tex,
%lowerbox=ignored,
redirectlowerto=\jobname-lower.tex,
]
upper $a + b = c$ \par
\lipsum[1][1-3]
\tcblower
lower $x + y = z$ \par
\lipsum[1][4-6]
\stepcounter{mycounter}
\end{tcolorbox}
\begin{minipage}{5cm}
\input{\jobname-lower.tex}
\end{minipage}
\bigskip
Value of \verb|mycounter|: \arabic{mycounter}
\end{document}
(Maybe that's because I have shorter memory and am less familiar with the code base.)
Though more efficient, the current implementation (see commit 4918308 (6.1.0, 2023-09-26)) makes it a bit harder to deduce the relationship between redirectlowerto
and lowerbox=ignored
, and to check whether redirectlowerto
will be reset or not by reset@core
style. Currently reset@core
contains lowerbox=visible, savelowerto=
.
Referring to lowerbox=ignored
in redirectlowerto
helps, for example
% either
redirectlowerto/.code={\pgfkeysalso{lowerbox=ignored}\tcb@redirectlowertotrue\def\kvtcb@savelowerto{#1}},
% or
redirectlowerto/.style={lowerbox=ignored,code={\tcb@redirectlowertotrue\def\kvtcb@savelowerto{#1}}},
For better readability, I will do this change. Thank you.
As far as I know, when the key
lowerbox=ignored
is used, the content of the so-called\lowerbox
is composed in a box, even if that box is not used. That may have side effects if there are instructions with side effects in that\lowerbox
.That may be a problem in particular when the key
savelowerto
is used (otherwise, the use of\lowerbox
withlowerbox=ignored
is rather point-less.Maybe there would be possible to add a key to disable the composition in a TeX box of the
\lowercase
...