0

Is there a way to have a savebox spanning multiple pages? In the following MWE, the text does not fit on a single page so it is pushed to the next page and the text is cut off.

\documentclass{article}
\usepackage{lipsum}
\newsavebox{\mybox}
\newcounter{test}
\begin{document}
\savebox{\mybox}{
  \setcounter{test}{10}
  \begin{minipage}{1.0\linewidth}
    \lipsum[1-10]
  \end{minipage}}
My counter is: \thetest.

\usebox{\mybox}
\end{document}

I know that I can define the command instead of using a savebox, however as the MWE shows, my usage of the savebox is necessary for my setup. In particular, I am using it as as a way to process the LaTex commands in the box (setcounter in this particular MWE), print some results, then output the content of the box.

Tohiko
  • 1,789
  • 2
    A box doesn't automatically break across pages. However, why you would need a box is not clear. You should probably explain better what you want to do and why a macro doesn't fulfill your needs. BTW, minipage creates a box; that doesn't break either (automatically). – frougon Feb 28 '20 at 23:54
  • 1
    A savebox can be split into smaller pieces, see https://tex.stackexchange.com/a/22836/194703. Yet, like @frougon, I do not really understand "however as the MWE shows, my usage of the savebox is necessary for my setup." Note also that tcolorbox has a library for breaking boxes over several pages. –  Feb 29 '20 at 01:29
  • your savebox consists of an hbox with two space characters (from missed %) then a full width vbox from the minipage. neither the vbox nor hbox can split over a page, the simplest solution here is simply to use the \lipsum text unboxed. You give no indication of why you want a box here. – David Carlisle Feb 29 '20 at 11:26
  • Thanks all. I was using the savebox to get the value of the counter the text in the box is rendered. I found out about totcount and I will use that instead. – Tohiko Feb 29 '20 at 12:08

2 Answers2

1

I think you're using the wrong tool. It would be better to rely on the \label-\ref mechanism.

Anyway, you can use a lower level approach, by setting a \vbox which is typeset and stored.

Then the box can be “unboxed”, but some tricks are necessary in order to preserve the leading.

\documentclass{article}
\usepackage{lipsum}

\newsavebox{\mybox}
\newcounter{test}

\newcommand{\mpbox}[1]{%
  \setbox\mybox=\vbox{
    \everypar{\strut\everypar{}}%
    #1\par\xdef\belowprevdepth{\the\prevdepth}
  }%
}
\newcommand{\usempbox}{%
  \par\edef\aboveprevdepth{\the\prevdepth}%
  \setbox\mybox=\vbox{\prevdepth\aboveprevdepth\unvbox\mybox}
  \unvbox\mybox\prevdepth\belowprevdepth
}
\begin{document}

\mpbox{
  \setcounter{test}{10}
    \lipsum[1-10]
}

My counter is: \thetest.

\usempbox
some other text

\end{document}

enter image description here

egreg
  • 1,121,712
0

Isn't \edef what you want, in order to capture the expansion of \thetest (i.e., the formatted counter value) at the time your macro is defined?

\documentclass{article}
\usepackage{lipsum}

\newcounter{test}
\setcounter{test}{10}

% Make sure the commands aren't already defined
\newcommand*{\myFirstCommand}{}
\newcommand*{\mySecondCommand}{}

\edef\myFirstCommand{%
  \unexpanded{%
    \lipsum[1-3]\par
    \begingroup\bfseries              % Just to put some emphasis
    My counter is: %
  }%
  \thetest.\endgroup
}

\stepcounter{test}

\edef\mySecondCommand{%
  \unexpanded{%
    \lipsum[4-8]\par
    \begingroup\bfseries              % Just to put some emphasis
    My counter is: %
  }%
  \thetest.\endgroup
}

% Just to show that this doesn't influence what \myFirstCommand and
% \mySecondCommand expand to.
\setcounter{test}{0}

\begin{document}

\myFirstCommand

\mySecondCommand

Blah. No more \verb|\bfseries| here.

\end{document}

enter image description here

frougon
  • 24,283
  • 1
  • 32
  • 55