Latex:将“评论”转换为进入“边注”

发布于 2024-08-20 23:28:52 字数 1032 浏览 3 评论 0原文

使用 LyX 我试图将“评论”转换为“边注”。

我尝试了几件事但没有运气。

最好的镜头是这样的:

\makeatletter

\@ifundefine{comment}{}{%

\renewenvironment{comment}[1]%

< code>{\begingroup\marginpar{\bgroup#1\egroup}}%

{\endgroup}}

\makeatother

或类似这样:

\ @ifundefined{comment}{}{%

\renewenvironment{comment}%

{\marginpar{}%

{}}%

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

IMAGE边注

我进行了很多搜索,试图找到如何解决这个问题,但没有运气。我找到了正在发生的事情的解释:

意外的输出 新字体中只有一个字符 您以为您更改了所选文本的字体,但只有第一个字符以新字体显示。 您很可能使用命令而不是声明。该命令应将文本作为其参数。如果不对文本进行分组,则只有第一个字符将作为参数传递。

我不知道也无法找到的是如何对文本进行分组。

希望有人能帮助我:-)

非常感谢。

最好的问候,

迭戈 (迪戈斯特克斯)

using LyX I'm trying to convert the "comments" into "marginal notes".

I tried several things but without luck.

The best shot was like this:

\makeatletter

\@ifundefined{comment}{}{%

\renewenvironment{comment}[1]%

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

{\endgroup}}

\makeatother

or like this:

\@ifundefined{comment}{}{%

\renewenvironment{comment}%

{\marginpar{}%

{}}%

But what I get is only the first character of the text converted. Like in this image:

IMAGE MARGINAL NOTE

I searched a lot trying to find how to solve this but without luck. I found the explanation of what is happening:

Unexpected Output
Only one character is in the new font
You thought you changed font over a selection of text, but only the first character has come out in the new font.
You have most probably used a command instead of a declaration. The command should take the text as its argument. If you don't group the text, only the first character will be passed as the argument.

What I don't know and wasn't able to find is how to group the text.

Hope someone could help me :-)

Many thanks.

Best Regards,

Diego
(diegostex)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

哽咽笑 2024-08-27 23:28:52

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

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 中,因为我们'将使用名称中带有 at 符号 (@) 的命令。

首先,我们创建一个盒子来存储材料:

\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}

Okay, let's walk through your (first) redefinition to see what's happening:

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

Here's how LaTeX is thinking about what you've told it:

\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

Note that xyzzy is the mandatory argument (#1). The contents of the environment ("This is the...") are inserted between lines 3 and 4.

If you write the following in your document:

\begin{comment}% <-- missing mandatory argument
  This is the contents of the environment.
\end{comment}

Then LaTeX will take the first token as the mandatory argument. In this case, the first token is T, the first character of the environment contents. So the letter T will be put in the margin and the remainder of the text will show up in a normal paragraph.

Okay, so to achieve what we want, the comment environment doesn't need any arguments. What we'll do is create a box, put the contents of the environment in that box, and then place that box in the margin.

Before we get started, if you're including this code in the preamble of a document, you'll need to wrap it all in \makeatletter and \makeatother since we'll be using commands with at signs (@) in their names.

First, let's create a box to store the material in:

\newsavebox{\marginbox}% contains the contents of the comment environment

Next, we'll start defining the comment environment. We'll set the environment begin and end commands to \relax. That way our \newenvironment command will be guaranteed to work.

\let\comment\relax% removes and previous definition of \begin{comment}
\let\endcomment\relax% removes any previous definition of \end{comment}

With that out of the way, we can define our new comment environment:

\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
}

Now, in your document, you can type:

\begin{comment}
  This is a comment that gets printed in the margin.
\end{comment}

So just for ease of copying and pasting, here's what a complete document would look like:

\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}
薄凉少年不暖心 2024-08-27 23:28:52

我认为你想要的是一个宏观而不是一个环境。
这是我一直使用的。宏定义:

\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 在它标记的文本中留下了一个微小的固定宽度菱形。

I think what you want is a macro and not an environment.
Here's what I use all the time. Macro definition:

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

Sample use:

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

I've done variations for some coauthors, e.g., the \remark leaves a tiny fixed-width diamond in the text that it marks.

小兔几 2024-08-27 23:28:52

我以同样的方式使用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}}

I use the LyX comments the same way, and found the following solution:

\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}}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文