我想在迭代中將資料保存到保存箱(或任何其他儲存元素)。最後,我想將每個資料點放入框架盒中,然後放入具有給定寬度的框架盒容器中。這是我的實作:
\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}
輸出:
如所見,使用程式實作時容器不會包裹內部框架框。怎麼了?
答案1
您希望 raggedright 允許行較短,並且需要取消裝箱保存的資料以允許換行:
\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}
答案2
我不確定你想使用盒子:
\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}
此指令\cleardata
重置新增框的機制。
這裡我etoolbox
為了方便使用了。您可以透過定義自己來避免它\appto
:
\makeatletter
\providecommand\appto[2]{%
\ifx#1\@undefined
\def#1{#2}%
\else
\expandafter\def\expandafter#1\expandafter{#1#2}%
\fi
}
\makeatother
答案3
您確定完成後的盒子需要 4 公分寬嗎?把這個寬度設定為裡面最寬的盒子的寬度不是更好嗎?如果是這樣,那麼您可以使用:
\newbox\foo
\def\savedata#1{\setbox\foo=\vbox{\unvbox\foo \hbox{\fbox{#1}}\kern1pt}\ignorespaces}
\def\printdata{\framebox{$\vcenter{\box\foo\kern-1pt}$}}