在新環境中使用new指令

在新環境中使用new指令

我想創建一個執行以下操作的環境newenvironment:它評估環境的​​高度,然後將其列印多次(文字高度/高度),以適應一頁。

以下是列印環境兩次的嘗試:

\documentclass{article}
\newenvironment{double}{\newcommand{\todouble}\bgroup}{\egroup\todouble\todouble}
\begin{document}
\begin{double}
    Hello, World!
\end{double}
\end{document}

我的第一個想法是使用\newenvironment來定義一個環境,​​該環境用於定義 包含環境內容的\newcommand命令。\todouble環境結束時\todouble被調用兩次。

但這失敗並出現錯誤:

! Extra }, or forgotten \endgroup.
\enddouble ->\egroup 
                 \todouble \todouble 
l.6 \end{double}

我該如何修復它?

答案1

您不能用於\bgroup分隔強制參數。你希望領導者將你所吸收的內容填滿這一頁。

\documentclass{article}

\newenvironment{double}
 {%
  \par % be in vertical mode
  \setbox0=\vbox\bgroup % start a box
  \strut % ensure good height for the first line
  \ignorespaces % ignore the end of line
 }
 {%
  \egroup % end the box
  \hrule height0pt % vertical analog of \leavevmode
  \cleaders\copy0\vfill % repeat as much as necessary
 }
\begin{document}
\begin{double}
    Hello, World!
\end{double}
\end{document}

在此輸入影像描述

擴展版本,檢查頁面中是否至少有兩份盒子的副本,如果不適合則僅列印盒子。

\documentclass{article}

\usepackage{lipsum}

\newenvironment{double}
 {%
  \par % be in vertical mode
  \setbox0=\vbox\bgroup % start a box
  \strut % ensure good height for the first line
  \ignorespaces % ignore the end of line
 }
 {%
  \ifhmode\strut\fi\egroup % end the box
  % see if at least two copies of the box fit in the page
  \ifdim\ht0 < \dimexpr .5\textwidth-4ex\relax
    \hrule height0pt % vertical analog of \leavevmode
    \cleaders\copy0\vfill % repeat as much as necessary
    \clearpage
  \else
    \unvbox0
    \clearpage
  \fi
 }
\begin{document}
\begin{double}
    Hello, World!
\end{double}

\begin{double}
\lipsum*[2]
\end{double}

\begin{double}
\lipsum
\end{double}
\end{document}

相關內容