Me gustaría guardar datos en una caja de almacenamiento (o cualquier otro elemento de almacenamiento) en iteraciones. Al final, me gustaría colocar cada punto de datos en un cuadro de marco y luego en un contenedor de cuadro de marco con un ancho determinado. Aquí está mi implementación:
\documentclass{article}
\newsavebox{\foo}
\newcommand{\savedata}[1]{\savebox{\foo}{\usebox{\foo} \fbox{#1}}}
\newcommand{\printdata}{\framebox{\parbox{4cm}{\usebox{\foo}}}}
\begin{document}
The desired usage:
\savedata{This is a box}
\savedata{This is another box}
\savedata{This is third box}
\printdata
\\
The desired output:
\framebox{\parbox{4cm}{
\fbox{This is a box}
\fbox{This is another box}
\fbox{This is third box}}}
\end{document}
Salida:
Como se ve, el contenedor no envuelve el cuadro de marco interno cuando se utiliza la implementación programática. ¿Lo que está mal?
Respuesta1
Quiere que raggedright permita que las líneas sean cortas y necesita desempaquetar los datos guardados para permitir el salto de línea:
\documentclass{article}
\newsavebox{\foo}
\newcommand{\savedata}[1]{\savebox{\foo}{\ifvoid\foo\else\unhbox\foo{} \fi\fbox{#1}}}
\newcommand{\printdata}{\framebox{\parbox{4cm}{\raggedright\unhbox\foo}}}
\begin{document}
The desired usage:
\savedata{This is a box}
\savedata{This is another box}
\printdata
The desired output:
\framebox{\parbox{4cm}{\raggedright\fbox{This is a box} \fbox{This is another box}}}
\end{document}
Respuesta2
No estoy seguro de que quieras usar cuadros:
\documentclass{article}
\usepackage{etoolbox}
\newcommand{\cleardata}{\renewcommand*{\saveddata}{}}
\newcommand*\saveddata{} % initialize
\newcommand{\savedata}[1]{%
\unskip
\ifx\saveddata\empty
\else
\appto{\saveddata}{\\}
\fi
\appto{\saveddata}{\fbox{#1}}\ignorespaces
}
\newcommand{\printdata}[1][4cm]{\fbox{\parbox{#1}{\raggedright\saveddata}}}
\begin{document}
The desired usage:
\savedata{This is a box}
\savedata{This is another box}
\savedata{This is third box}
\printdata
The desired output:
\framebox{\parbox{4cm}{
\fbox{This is a box} \\
\fbox{This is another box} \\
\fbox{This is third box}}}
With optional argument: \printdata[6cm]
\end{document}
El comando \cleardata
restablece el mecanismo para agregar nuevos cuadros.
Aquí lo usé etoolbox
por conveniencia. Puedes evitarlo definiéndote \appto
a ti mismo:
\makeatletter
\providecommand\appto[2]{%
\ifx#1\@undefined
\def#1{#2}%
\else
\expandafter\def\expandafter#1\expandafter{#1#2}%
\fi
}
\makeatother
Respuesta3
¿Estás seguro de que necesitas 4 cm de ancho de la caja completa? ¿No es mejor establecer este ancho como el ancho del cuadro más ancho del interior? Si es así, entonces puedes usar:
\newbox\foo
\def\savedata#1{\setbox\foo=\vbox{\unvbox\foo \hbox{\fbox{#1}}\kern1pt}\ignorespaces}
\def\printdata{\framebox{$\vcenter{\box\foo\kern-1pt}$}}