Latex:将“注释”转换为“边注”

4

我正在使用LyX尝试将“注释”转换为“边注”。

我尝试了几种方法,但都没有成功。

最好的一次尝试是这样的:

\makeatletter

\@ifundefined{comment}{}{%

\renewenvironment{comment}[1]%

{\begingroup\marginpar{\bgroup#1\egroup}}%

{\endgroup}}

\makeatother

或者像这样:

\@ifundefined{comment}{}{%

\renewenvironment{comment}%

{\marginpar{}%

{}}%

但我得到的只是文本的第一个字符被转换。就像这张图片中所示:

IMAGE MARGINAL NOTE

我搜索了很多,但没有找到解决方法。我找到了发生的原因:
意外输出 新字体中只有一个字符 您认为已更改文本选择的字体,但只有第一个字符以新字体出现。 您很可能使用了命令而不是声明。该命令应将文本作为其参数。如果您不对文本进行分组,则只会传递第一个字符作为参数。
我不知道如何对文本进行分组,并且也无法找到答案。
希望有人能帮助我 :-)
非常感谢。
最好的问候,
Diego(diegostex)
3个回答

5

好的,让我们来看一下你(第一个)重新定义发生了什么:

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对您提供的信息的处理方式:

这是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环境。我们将把环境的开始和结束命令设置为\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
我很高兴它有所帮助。我认为你的问题归结于对\renewenvironment命令工作方式的轻微误解;当我第一次遇到它时,它也让我感到困惑,因为环境的内容不是由#1指定的--相反,你只需编写“环境内容之前”的命令和“环境内容之后”的命令即可。 - godbyk

4

我认为您需要的是宏而不是环境。这是我经常使用的宏定义:

\def\remark#1{\marginpar{\raggedright\hbadness=10000
    \def\baselinestretch{0.8}\tiny
    \it #1\par}}

示例用法:

\remark{Interesting comparisons with the 
   internal language of \citet{harper:type-theoretic}}

我已经为一些合作者做了变体,例如,\remark 在标记文本时会留下一个微小的固定宽度的菱形。


Norman,非常感谢您迅速的回复。是的,我也使用类似的东西,但是特别想重新定义“comment”环境为边注,因为我想在LyX中能够使用“搜索下一个注释”,并且将注释打印在侧边栏上,这样它们就不会改变主文本的流程。我认为唯一的方法是更新注释环境,但我不知道如何对注释的文本进行分组,这样我就可以将所有文本而不仅仅是第一个字母转换。 - diegos
@diagos:我看到{lrbox}环境是这个故事的主角。干得好,@godbyk! - Norman Ramsey

1

我也是用LyX注释的方式,找到了以下解决方案:

\usepackage{environ}
\let\comment\relax% removes and previous definition of \begin{comment}
\let\endcomment\relax% removes any previous definition of \end{comment}
\NewEnviron{comment}{\marginpar{\BODY}}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接