好吧,讓我們通過你的(第一)重新定義走走看看發生了什麼:
1 \@ifundefined{comment}{}{% only do this if the comment environment has been defined
2 \renewenvironment{comment}[1]% redefining a 'comment' environment with one mandatory argument
3 {\begingroup\marginpar{\bgroup#1\egroup}}% put the mandatory argument inside a marginpar
4 {\endgroup}}% close the environment
這裏的乳膠是怎麼想約你告訴它:
\begin{comment}{xyzzy}% <-- note the mandatory argument (runs line 3)
This is the contents of the environment.
\end{comment}% <-- this is where LaTeX runs line 4
注意xyzzy
是強制性參數(#1
)。環境(「This is the
...」)的內容插入線3和4
如果你寫你的文檔中的下列之間:
\begin{comment}% <-- missing mandatory argument
This is the contents of the environment.
\end{comment}
然後乳膠會第一令牌作爲強制性論據。在這種情況下,第一個令牌是T
,這是環境內容的第一個字符。因此,字母T
將被放入頁邊空白中,其餘部分將顯示在正常段落中。
好吧,爲了達到我們想要的效果,comment
環境不需要任何參數。我們要做的是創建一個框,將環境內容放入該框中,然後將該框放在邊框中。在我們開始之前,如果您將此代碼包含在文檔的前言中,則需要將它們全部包裝在\makeatletter
和\makeatother
中,因爲我們將在其中使用帶有符號(@
)的命令名。
首先,讓我們創建一個盒子把材料儲存在:
\newsavebox{\marginbox}% contains the contents of the comment environment
接下來,我們將開始定義comment
環境。我們將環境開始和結束命令設置爲\relax
。這樣我們的\newenvironment
命令將保證工作。
\let\comment\relax% removes and previous definition of \begin{comment}
\let\endcomment\relax% removes any previous definition of \end{comment}
有了這樣的方式,我們可以定義我們的新comment
環境:現在
\newenvironment{comment}{%
\begin{lrbox}{\marginbox}% store the contents of the environment in a box named \marginbox
\begin{minipage}{\marginparwidth}% create a box with the same width as the marginpar width
\footnotesize% set any font or other style changes you'd like
}{% the following lines are for the \end{comment} command
\end{minipage}% close the minipage
\end{lrbox}% close the box
\marginpar{\usebox{\marginbox}}% typeset the box in the margin
}
,你的文檔中,你可以輸入:
\begin{comment}
This is a comment that gets printed in the margin.
\end{comment}
所以只是爲了便於複製和粘貼,以下是一個完整的文檔:
\documentclass{article}
\makeatletter
\newsavebox{\marginbox}% contains the contents of the comment environment
\let\comment\relax% removes and previous definition of \begin{comment}
\let\endcomment\relax% removes any previous definition of \end{comment}
\newenvironment{comment}{%
\begin{lrbox}{\marginbox}% store the contents of the environment in a box named \marginbox
\begin{minipage}{\marginparwidth}% create a box with the same width as the marginpar width
\footnotesize% set any font or other style changes you'd like
}{% the following lines are for the \end{comment} command
\end{minipage}% close the minipage
\end{lrbox}% close the box
\marginpar{\usebox{\marginbox}}% typeset the box in the margin
}
\makeatother
\usepackage{lipsum}% just provides some filler text
\begin{document}
Hello, world!
\begin{comment}
This is a comment that gets printed in the margin.
\end{comment}
\lipsum
\end{document}
嗨Godbyk,優秀!!!!非常感謝它正是我所需要的。 此外,非常感謝您花時間詳細解釋這個問題,它幫助我瞭解正在發生的事情以及如何使用該框來存儲文本。我可以使用它的很多新的技巧:-)))) 乾杯, 迭戈 – diegos 2010-02-14 02:18:12
我很高興它幫助。我認爲你的問題歸結爲對'\ renewenvironment'命令如何工作的輕微誤解;當我第一次遇到它時,它也拋出了一點,因爲環境的內容沒有被'#1'指定 - 相反,你只需要在'環境內容'命令和'環境內容'命令之前寫'。 – godbyk 2010-02-14 09:39:12
不錯的工作;我不知道'{lrbox}'。 +1 – 2010-02-14 16:16:16