我想在页面左边距放置一小段文字,其中包含代码/命令列表的小描述,在主页上使用包列出代码listing
。
但是我放在左边距的描述文字总是比列表开始时的要高,并且不对齐。
我希望左边距文本在同一垂直水平,或者在中间。现在看起来不太好。这就是现在的情况。任何其他不使用此包的解决方案也可以。
所以我希望它看起来像
或者在中间:
两种方式都可以
下面是 MWE。TL 2023 带有 lualatex 编译器。
\documentclass{article}
\usepackage{listings}
\usepackage{fancybox,fancyvrb,xcolor}
\usepackage{marginnote}
\definecolor{bg}{RGB}{255,255,226}
\lstdefinestyle{TEXT}{%
basicstyle=\ttfamily\normalsize,
breaklines=false,
columns=fullflexible,
keepspaces=true,
backgroundcolor=\color{bg},
rulecolor=\color{gray},
language=,
frame=single,
frameround=tttt,
aboveskip=12pt,belowskip=6pt
}
\lstnewenvironment{TEXTinline}{%
\lstset{style=TEXT}}{}
\begin{document}
\reversemarginpar
%Use \normalmarginpar to switch back.
\marginnote{Command}[0cm]
\begin{TEXTinline}
This is my long command
which can be very very very
long command
used to process the file
\end{TEXTinline}
\marginnote{Command}[0cm]
\begin{TEXTinline}
This is another of my long commands
used to process the file
\end{TEXTinline}
\end{document}
将边注放在列表后面并没有帮助。
\begin{TEXTinline}
This is another of my long commands
used to process the file
\end{TEXTinline}
\marginnote{Command}[0cm] %now it is below the listings
更新
我必须aboveskip=12pt
自动在列表上方留出垂直空间,这样我就不必手动记住这样做了。以下是将列表更改为以下版本时显示的意思aboveskip=0
\documentclass{article}
\usepackage{listings}% added lstautogobble oct 30,2020
\usepackage{fancybox,fancyvrb,xcolor}
\usepackage{marginnote}
\definecolor{bg}{RGB}{255,255,226}
\lstdefinestyle{TEXT}{%
basicstyle=\ttfamily\normalsize,
breaklines=false,
columns=fullflexible,
keepspaces=true,
backgroundcolor=\color{bg},
rulecolor=\color{gray},
language=,
frame=single,
frameround=tttt,
aboveskip=0pt,belowskip=6pt
}
\lstnewenvironment{TEXTinline}{%
\lstset{style=TEXT}}{}
\begin{document}
\reversemarginpar
This is my code
\marginnote{Command}[0cm]
\begin{TEXTinline}
This is my long command
which can be very very very
long command
used to process the file
\end{TEXTinline}
\end{document}
给予
所以现在我必须修复上述问题,以便主文本和列表之间的垂直空间正确。但这可能更容易修复。但这意味着我必须记住每次使用列表时添加明确的垂直空间。以前,这是自动完成的。
但现在我知道问题所在了,所以我只需要记住每次手动添加垂直空间。像这样?
This is my code
\vspace{12pt}
%
\marginnote{Command}[0cm]
\begin{TEXTinline}
This is my long command
which can be very very very
long command
used to process the file
\end{TEXTinline}
但现在问题又回来了:
因此这个解决方案似乎不起作用。
答案1
虽然@cfr 已经给出了答案。我想我还是想为你发布另一个解决方案。正如 cfr 的答案所建议的那样,第二个选项是您希望边注位于插入点的垂直空间。这很有效,因为您可以手动调整边注的位置。但是,您需要每次计算距离并将边注放在合适的位置。
还有另一种解决方案,如果您使用默认\marginpar{}
命令。通过为列表环境设置一个escapechar
。并将\marginpar{}
列表环境内的转义字符放在转义字符之间。这样,列表环境仍将以正常方式处理命令,而不是将它们显示为动词内容。边注将在您插入命令的确切行生成。
下面是显示两种解决方案差异的示例:
\documentclass{article}
\usepackage{listings}
\usepackage{fancybox,fancyvrb,xcolor}
\usepackage{marginnote}
\usepackage{lipsum}
\definecolor{bg}{RGB}{255,255,226}
\lstdefinestyle{TEXT}{%
basicstyle=\ttfamily\normalsize,
breaklines=false,
columns=fullflexible,
keepspaces=true,
backgroundcolor=\color{bg},
rulecolor=\color{gray},
language=,
frame=single,
frameround=tttt,
aboveskip=12pt,belowskip=6pt,
escapechar=\@, %everything in between @ and @ will be processed as a normal way
}
\lstnewenvironment{TEXTinline}{%
\lstset{style=TEXT}}{}
\begin{document}
\reversemarginpar
%Use \normalmarginpar to switch back.
\lipsum[1]
\marginnote{Command}[1.3cm]
\begin{TEXTinline}
This is my long command
which can be very very very
long command
used to process the file
\end{TEXTinline}
\lipsum[1]
\begin{TEXTinline}
This is another of my long commands
which can be very very very
@\marginpar{Command}@long command
used to process the file
\end{TEXTinline}
\end{document}
答案2
我并没有建议将列表上移。我建议将注释下移。
\documentclass{article}
\usepackage{listings}
\usepackage{fancybox,fancyvrb,xcolor}
\usepackage{marginnote}
\definecolor{bg}{RGB}{255,255,226}
\lstdefinestyle{TEXT}{%
basicstyle=\ttfamily\normalsize,
breaklines=false,
columns=fullflexible,
keepspaces=true,
backgroundcolor=\color{bg},
rulecolor=\color{gray},
language=,
frame=single,
frameround=tttt,
aboveskip=12pt,belowskip=6pt
}
\lstnewenvironment{TEXTinline}{%
\lstset{style=TEXT}}{}
\begin{document}
\reversemarginpar
%Use \normalmarginpar to switch back.
\marginnote{Command}[12pt]
\begin{TEXTinline}
This is my long command
which can be very very very
long command
used to process the file
\end{TEXTinline}
\marginnote{Command}[12pt]
\begin{TEXTinline}
This is another of my long commands
used to process the file
\end{TEXTinline}
\end{document}
加上康德: