では、(最初の) 再定義を見て、何が起こっているか見てみましょう:
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
LaTeX は、あなたが伝えたことについて次のように考えています。
\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}
次に、LaTeX は最初のトークンを必須の引数として受け取ります。この場合、最初のトークンはT
、環境コンテンツの最初の文字です。そのため、文字T
は余白に配置され、残りのテキストは通常の段落に表示されます。
さて、私たちが望むものを達成するために、comment
環境は引数を必要としません。ボックスを作成し、環境の内容をそのボックスに入れ、そのボックスを余白に配置します。
開始する前に、このコードをドキュメントのプリアンブルに含める場合は、すべてをラップする必要があります\makeatletter
。これは、名前に\makeatother
アットマーク ( ) を含むコマンドを使用するためです。@
まず、マテリアルを格納するボックスを作成しましょう。
\newsavebox{\marginbox}% contains the contents of the comment environment
次に、comment
環境の定義を開始します。環境の begin コマンドと end コマンドを に設定します\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}