0

Suppose you want to temporarily redefine a macro in which you use the original. Is there a general recipe to remember the original definition, use the original in the temporary one, and restore the original afterwards?

Here is one real problem I'm trying to solve:

\begin{figure}
\ContinuedFloat
% . . . some figure . . .
% ??? Preserve the original ???
\renewcommand{\thefigure}{\originalTheFigure~(continued)}
\caption{} % -> prints "Figure 3 (continued)"
\renewcommand{\thefigure}{\originalTheFigure}

To this particular example, there may be (and are) other solutions but I thought the technique can be used for many other problems.

Ryo
  • 871
  • Have you tried sandwiching the macro redefinition and its use in a \begingroup .... \endgroup TeX group? – Mico Feb 01 '23 at 05:13
  • @Mico Thanks for your help, but it seems to me that you miss my requirement to use the original macro within the new one. \renewcommand{\thefigure}{use \thefigure here} causes infinite recursion. Before the redefinition, we need to store the original definition in a different name. – Ryo Feb 01 '23 at 05:37
  • 2
    \NewCommandCopy\originalTheFigure\thefigure\renewcommand\thefigure{\originalTheFigure~(continued)}. If you absolutely know the structure of \thefigure is just a \newcommand\thefigure{<stuff>} you can also use \let. Scope the thing with a group, (so instead of your \renewcommand after the \caption usage put a \begingroup in front of the \NewCommandCopy and an \endgroup after the \caption). – Skillmon Feb 01 '23 at 06:15
  • @Ryo - Thanks, but it seems to me that you may have missed the power of using a TeX group. Since the default definition of \thefigure in most document classes is just \arabic{figure}, what's wrong about \renewcommand{\thefigure}{\arabic{figure} (continued)}, sandwiched between \begingroup and \endgroup directives? – Mico Feb 01 '23 at 08:07
  • @Mico I didn't miss the power of \begingroup! Above your lat comment, @Skillmon shows a complete solution using \begingroup and \endgroup. I tried the solution and it works. Since he or she didn't post it as an answer, I meant to post it as an answer later. – Ryo Feb 01 '23 at 18:02
  • 1
  • @Skillmon Despite your suggestion does formally answer the question, redefining \thefigure would not have effects for the caption only, but also for the entry to the list of figures and, e.g., for \label/\ref. In the list of figures the “(continued)” would also be part of the number and therefore needs a redefinition of \l@figure with a very large number width, that would also influence the entries without “(continued)”. So IMHO at least the continue captions should not create entries to the list of figures. This is why I would prefer another suggestions (see my answer). – cabohah Feb 02 '23 at 07:55
  • @cabohah not part of the specification :P but yes, you're right, you'd get an entry in the Lo(T|F) with just using \caption{}. – Skillmon Feb 02 '23 at 08:03
  • @user202729 I looked at the thread you refer to before posting my question here because that thread does not touch upon how to restore the original definition. – Ryo Feb 02 '23 at 12:28

2 Answers2

1

If you'd not use package caption redefining \thefigure would not result in “Figure 3 (continued)” but “Figure 3 (continued):” (please note the colon at the end). This does not happen with package caption, but nevertheless I would suggest to define a command \ContinuedCaption, that changes the labelformat instead of redefining \thefigure:

\documentclass{article}

\usepackage{caption}

\newcommand*{\ContinuedCaption}{% \ContinuedFloat% Because \ContinuedCaption makes only sense inside continued floats \begingroup% only needed, if \ContinuedCaption should be mixed with standard \caption in the same float \captionsetup{labelsep=space}% \caption[]{(continued)}% \endgroup% to end the group started with \begingroup and therefore the effect of \captionsetup }

\usepackage{mwe}

\begin{document} \listoffigures

\section{Test} \blindtext

\begin{figure}[ht!] \centering \includegraphics{example-image-a} \caption{This is an example caption for the first part of the example image} \end{figure}

\blindtext

\begin{figure} \centering \includegraphics{example-image-b} \ContinuedCaption \end{figure}

\end{document}

I've encapsulated the change of labelformat to a local group. So the effect is limited to the usage of \ContinuedCaption.

The continued figure now would be:

continued figure with “Figure 1 (continued)”

Using an empty optional argument for \caption[]{(continued)} as shown, does prevent adding an entry into the list of figures. If you'd remove it, there would be an entry “Figure 1 (continued)” also in the list of figures. You could also add an optional argument to the definition, used only for an optional entry to the list of figures:

\newcommand*{\ContinuedCaption}[1][]{%
  \ContinuedFloat% Because \ContinuedCaption makes only sense inside continued floats
  \begingroup
    \captionsetup{labelsep=space}%
    \caption[{#1}]{(continued)}%
  \endgroup
}

or

\newcommand*{\ContinuedCaption}[1][\emph{(continued)}]{%
  \ContinuedFloat% Because \ContinuedCaption makes only sense inside continued floats
  \begingroup
    \captionsetup{labelsep=space}%
    \caption[{#1}]{(continued)}%
  \endgroup
}

However, if you insist in changing \thefigure, you could add this to the definition of \ContinuedCaption:

\documentclass{article}
\usepackage{caption}

\newcommand{\ContinuedFigureCaption}{% \ContinuedFloat% Because \ContinuedCaption makes only sense inside continued floats \begingroup \NewCommandCopy\OriginalTheCaption\thefigure \renewcommand\thefigure{\OriginalTheCaption~(continued)}% % \captionsetup{labelsep=space}% not needed because package caption does this with empty captions automatically \caption[]{}% empty optional argument needed (see below) \endgroup }

\usepackage{mwe}

\begin{document} \listoffigures

\section{Test} \blindtext

\begin{figure}[ht!] \centering \includegraphics{example-image-a} \caption{This is an example caption for the first part of the example image} \end{figure}

\blindtext

\begin{figure} \centering \includegraphics{example-image-b} \ContinuedFigureCaption \end{figure}

\end{document}

However, this definition is less universal, because it cannot be used, e.g., for tables without changes. Without the optional argument [] for \caption, it also would be problematic for the list of figures, because now “(continued)” is part if the number and the space reserved for numbers in the list of figures is limited. So it would result in an unwanted overlapping of the text and the dots:

list of figures entry issue

So you would also have to redefine \l@figure, if you want (optional) entries to the list of figures. But than you would have a large gap between the number and the text also for main figure entries. So I would not recommend to use a redefinition of \thefigure at least if you want entries for the continues to the list of figures.

The first suggestion is much more general, because it can be used not only for figure but also for table (and other floats). And it does not result in issues with the entries to the list of figures or list of tables.

Note: \NewCommandCopy needs a to old LaTeX kernel. If you are using an old LaTeX, you could replace it (in this case) by \let. But I would suggest to update LaTeX instead.

cabohah
  • 11,455
  • Thanks! But I do get "Figure 3 (continued)" without a colon by redefining the \thefigure macro. I'll investigate what's the difference from you and report back later. Regarding old latex, you don't have to worry. I keep updating to latest texlive and I keep running tlmgr almost daily. – Ryo Feb 01 '23 at 17:58
  • @Ryo Maybe package caption is smarter than expected. However, you will have the issue with the entry to the list of figures, I've described. So I still would recommend my first suggestion. – cabohah Feb 01 '23 at 19:03
1

I'll post skillmon's answer in her/his comment above:

  1. Delimit the section with \begingroup and \endgroup.
  2. Within the section, use \NewCommandCopy to remember the original definition and redefine the macro using the copy of the original macro.
  3. After \endgroup, the original definition is automatically restored.

mico also helped me in her/his comment above.

Below is how the solution is implemented for the example given in the Question post. (But, of course, cobohah's solution is much better for this particular example.)

\documentclass{article}
\usepackage{caption}
\begin{document}
\begin{figure}
\caption{First part}
\end{figure}
\begin{figure}
  \ContinuedFloat
  \begingroup
  \NewCommandCopy{\originalTheFigure}{\thefigure}
  \renewcommand{\thefigure}{\originalTheFigure~(continued)}
  \caption{}% Empty
  \endgroup
\end{figure}
\end{document}
Ryo
  • 871
  • If you'd add \listoffigures to your document, you would see, that this is problematic as I told you after the second example in my answer. See the text overlapping with the dots in https://i.imgur.com/STpnSM9.png – cabohah Feb 02 '23 at 07:29
  • BTW: In the shown example \begingroup and \endgroup are not needed, because the redefined \thefigure would automatically end with \end{figure} which also ends a group. – cabohah Feb 02 '23 at 07:46
  • @cabohah That's the very reason why I was reluctant to include an example in my original question. I also said that there are solutions without redefining \thefigure. My question was not: what's the best method to produce "Figure 3 (continued)" ? – Ryo Feb 02 '23 at 12:31
  • @cabohah I also clearly said that your solution is much better than what I show above. The reader will see your answer for a better solution to this particular caption problem. I merely posted an answer to the question I posted. – Ryo Feb 02 '23 at 12:41