D'accord, nous allons marcher à travers votre (première) redéfinition pour voir ce qui se passe:
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
Voilà comment LATEX pense à ce que vous avez dit qu'il:
\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
Notez que xyzzy
est l'argument obligatoire (#1
). Le contenu de l'environnement (« This is the
... ») sont insérés entre les lignes 3 et 4.
Si vous écrivez ce qui suit dans votre document:
\begin{comment}% <-- missing mandatory argument
This is the contents of the environment.
\end{comment}
Ensuite LATEX prendra le premier jeton comme argument obligatoire. Dans ce cas, le premier jeton est T
, le premier caractère du contenu de l'environnement. Donc la lettre T
sera mise dans la marge et le reste du texte apparaîtra dans un paragraphe normal. D'accord, donc pour réaliser ce que nous voulons, l'environnement comment
n'a pas besoin d'arguments. Ce que nous allons faire est de créer une boîte, mettre le contenu de l'environnement dans cette boîte, puis placez cette boîte dans la marge.
Avant de commencer, si vous incluez ce code dans le préambule d'un document, vous aurez besoin de l'envelopper le tout dans \makeatletter
et \makeatother
puisque nous allons utiliser les commandes avec au signe (@
) dans leur des noms.
Tout d'abord, nous allons créer une boîte pour stocker le matériel dans:
\newsavebox{\marginbox}% contains the contents of the comment environment
Ensuite, nous allons commencer à définir l'environnement comment
. Nous allons définir les commandes begin et end de l'environnement sur \relax
. De cette façon, notre commande \newenvironment
sera garantie pour fonctionner.
\let\comment\relax% removes and previous definition of \begin{comment}
\let\endcomment\relax% removes any previous definition of \end{comment}
Avec cela de la manière, nous pouvons définir notre nouveau comment
environnement:
\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
}
Maintenant, dans votre document, vous pouvez taper:
\begin{comment}
This is a comment that gets printed in the margin.
\end{comment}
Alors que pour la facilité de copier et coller, voici à quoi ressemblerait un document complet:
\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}
Salut godbyk, excellent !!!! Merci beaucoup, il fait exactement ce dont j'avais besoin. Merci beaucoup de prendre le temps pour cette explication détaillée, cela m'a aidé à comprendre ce qui se passait et comment utiliser la boîte pour stocker du texte. Je peux l'utiliser pour beaucoup de nouvelles astuces :-)))) Cheers, Diego – diegos
Je suis heureux que cela a aidé. Je pense que votre problème se résume à une légère incompréhension de la façon dont la commande '\ renewenvironment' a fonctionné; il m'a lancé un peu quand je l'ai rencontré pour la première fois, puisque le contenu de l'environnement n'est pas spécifié par un '# 1' - à la place, vous écrivez simplement les commandes 'before environment contents' et 'after environment contents'. – godbyk
Beau travail; Je ne savais pas à propos de '{lrbox}'. +1 –