Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I would like to take the contents of a box generated with \begin{lrbox} ... \end{lrbox} and write it to a file.

The use case is a question and answer environment, where the answer should be collected and showed at different location in the document. In addition I would like to show the answer next to the question. Therefore I pack the question into a box, and then I assume that the contents of this box can both be used inside the environment and for storing the data in an external file. But the last part is the reason for this question.

The approach I have used this far is the newfile package and the code below. The problem is that what is written to test3.comment is the following

\unhbox \voidb@x \copy \myboxquestion \relax 

Almost working code

\documentclass{article}
\usepackage{newfile}
\newoutputstream{comment}
\openoutputfile{\jobname.comment}{comment}

\newsavebox{\myboxquestion}
\newenvironment{question}{%
\begin{lrbox}{\myboxquestion}\begin{minipage}{\linewidth}}{%
\end{minipage}\end{lrbox} \par\usebox{\myboxquestion}
% What to do here?
\addtostream{comment}{\usebox{\myboxquestion}}}

\setlength{\parindent}{0.0cm}

\begin{document}

Testing
\begin{question}
What is 3 + 4
\end{question}


\section{Collected}
\input{\jobname.comment}

\end{document}
share|improve this question
1  
You might also want to take a look at the tcolorbox package. Mostly known for its beautiful boxes, it also provides concepts for splitting questions and answers as you want, with special focus on verbatim material (see §5.4 of the documentation). –  Daniel Aug 17 '12 at 15:39
    
I was not aware of the tcolorbox package. It looks very interesting and I will need to take a closer look at it. Thanks for the recommendation. –  midtiby Aug 18 '12 at 8:45
add comment

2 Answers

up vote 16 down vote accepted

TeX does not have a serialisation of its box structures that may be accessed, in particular it can not be written to a file. You either need to save the box within TeX memory for re-use or instead of saving the box, grab the tokens that make up the text and write those to a file to be re-set when the file is read in. That is how tables of contents and endnotes packages work to repeat the section headings or footnotes at the start or end of a document

Here is the MWE using a global box register rather than external file.

enter image description here

\documentclass{article}

\newsavebox{\myboxquestion}
\newbox\savedqns
\setbox\savedqns\vbox{}

\newenvironment{question}{%

\begin{lrbox}{\myboxquestion}\begin{minipage}{\linewidth}}{%
\end{minipage}\end{lrbox} \par\noindent\usebox{\myboxquestion}
\global\setbox\savedqns\vbox{%
\unvbox\savedqns
\bigskip
\filbreak
\noindent\usebox{\myboxquestion}}}

\setlength{\parindent}{0.0cm}

\begin{document}

Testing
\begin{question}
What is 3 + 4
\end{question}


Testing again
\begin{question}
What is 3 + 3
\end{question}


\section{Collected}

\unvbox\savedqns

\end{document}
share|improve this answer
    
This code was easily adaptable to my problem. Thanks for the input. –  midtiby Aug 17 '12 at 13:15
add comment

As David already said, the contents of boxes cannot be written to a file. However the contents of the environment can be caught using the package environ and stored in a macro that can be called and can also be written to a file.

BTW, before reading the output stream, it should be closed first.

\documentclass{article}

\usepackage{newfile}
\newoutputstream{comment}
\openoutputfile{\jobname.comment}{comment}

\usepackage{environ}
\NewEnviron{question}{%
  \BODY
  \addtostream{comment}{\detokenize\expandafter{\BODY}}%
  % \detokenize prevents macro expansion
}

\setlength{\parindent}{0cm}

\begin{document}

Testing

\begin{question}
What is 3 + 4?
\end{question}

\section{Collected}
\closeoutputstream{comment}
\input{\jobname.comment}

\end{document}

Result:

enter image description here

Drawback of this method is verbatim stuff inside the contents of the environment. This could be supported by catching the environment contents in verbatim manner. In this form it can be written to a file. The catched environment can be set by the help of \scantokens.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.