首先,我给 verbatim 环境添加了一个缩进,代码如下:
\documentclass{article}
\begin{document}
Some text before the verbatim environment.
\begin{verbatim*} % <--indented verbatim environment
This is a verbatim environment containing some code or text. % <--indented verbatim environment
\end{verbatim*} % <--indented verbatim environment
Some text after the verbatim environment.
\end{document}
为了更好的描述问题,我使用了verbatim*
可以输出空白符的环境,编译结果显示输出以下内容前多了一个空行,如下图(红色圆圈区域):
好吧,当我删除缩进时,空行就消失了,代码和编译结果如下所示:
\documentclass{article}
\begin{document}
Some text before the verbatim environment.
\begin{verbatim*}
This is a verbatim environment containing some code or text.
\end{verbatim*}
Some text after the verbatim environment.
\end{document}
有人能解释上述现象吗?
答案1
LaTeX 不是 Java 或 C。对于习惯使用编程语言的人来说,缩进源代码似乎很有吸引力,但 TeX 的目标是排版,所以空格是非常重要,而在 Java 或 C 源代码中,它们大多被忽略,除非用于分隔关键字。
然而,TeX 中的一些空格通常会被忽略,即行首的空格。除了,现在你可以猜了,在verbatim
块内。假设你想复制
class Main {
public static void main(String[] args) {
int first = 10;
int second = 20;
// add two numbers
int sum = first + second;
System.out.println(first + " + " + second + " = " + sum);
}
}
您不希望忽略行首的空格,是吗?
我该如何输入这些内容
整个环境缩进四个空格document
是完全没有必要的,而且会缩短宝贵的屏幕空间。我认为,空行更有效。
\begin{document}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum
gravida mauris. Nam arcu libero, nonummy eget, consectetuer id,
vulputate a, magna.
\begin{verbatim*}
class Main {
public static void main(String[] args) {
int first = 10;
int second = 20;
// add two numbers
int sum = first + second;
System.out.println(first + " + " + second + " = " + sum);
}
}
\end{verbatim*}
Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi.
Morbi auctor lorem non justo. Nam lacus libero, pretium at, lobortis
vitae, ultricies et, tellus. Donec aliquet, tortor sed accumsan
bibendum, erat ligula aliquet magna, vitae ornare odio metus a mi.
\end{document}
如果你缩进verbatim
TeX 无法猜测您从多少缩进开始,因为一旦\begin{verbatim*}
被扫描,它之前的空格就会被忽略。
在verbatim*
环境中,行首的空格不是忽略,因为您想保留缩进。 之前的空格也是如此\end{verbatim*}
。
\begin{document}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum
gravida mauris. Nam arcu libero, nonummy eget, consectetuer id,
vulputate a, magna.
\begin{verbatim*}
class Main {
public static void main(String[] args) {
int first = 10;
int second = 20;
// add two numbers
int sum = first + second;
System.out.println(first + " + " + second + " = " + sum);
}
}
\end{verbatim*}
Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi.
Morbi auctor lorem non justo. Nam lacus libero, pretium at, lobortis
vitae, ultricies et, tellus. Donec aliquet, tortor sed accumsan
bibendum, erat ligula aliquet magna, vitae ornare odio metus a mi.
\end{document}
有出路吗?
是的,只是不要过度缩进源代码。或者,如果您不想遵循此建议,请加载fancyvrb
。
\documentclass{article}
\usepackage{fancyvrb}
\begin{document}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum
gravida mauris. Nam arcu libero, nonummy eget, consectetuer id,
vulputate a, magna.
\begin{Verbatim*}[gobble=8]
class Main {
public static void main(String[] args) {
int first = 10;
int second = 20;
// add two numbers
int sum = first + second;
System.out.println(first + " + " + second + " = " + sum);
}
}
\end{Verbatim*}
Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi.
Morbi auctor lorem non justo. Nam lacus libero, pretium at, lobortis
vitae, ultricies et, tellus. Donec aliquet, tortor sed accumsan
bibendum, erat ligula aliquet magna, vitae ornare odio metus a mi.
\end{document}
答案2
您可以使用verbatimbox
来避免该问题……如果需要,甚至可以使逐字内联。
\documentclass{book}
\usepackage{tcolorbox,verbatimbox}
\begin{document}
\begin{tcolorbox}
\begin{verbbox}C:\Users\me\Programs> test.cpp\end{verbbox}
guess what?\\
\hspace*{1em}\theverbbox\\
the verbatim environment generate an additional empty line at the end as shown in the picture below.
\end{tcolorbox}
\end{document}
答案3
缩进\begin{verbatim}
不是问题。无论前面是否有空格或/和黑线,逐字文本都会从新行开始。点。
但是缩进\end{verbatim}
必须在该环境内进行,因此每个空格或制表符都算数...
\documentclass{article}
\begin{document}
\subsubsection*{proof of the concept}
So, respect this:\begin{verbatim*}foo\end{verbatim*}
What happen before have not any effect :
\begin{verbatim*}
foo
\end{verbatim*}
But the indentation {\em inside} matters:
\begin{verbatim*}
foo
\end{verbatim*}
And it also matters how you do the indentation:
\begin{verbatim*}
foo
\end{verbatim*}
\end{document}
除此之外,还要注意,如果后面\end{verbatim}
有一个空行,也很重要。逐字文本的结尾总是意味着换行,以及一些垂直跳跃,加上正常的段落垂直跳跃(只有当\parskip
不是 0pt 时你才会注意到,例如,\usepackage[indent,skip=1em]{parskip}
在序言中使用),但不是新段落开头的缩进,除非后面\end{verbatim}
跟着一个\par
或空行(即真正的段落结束):
\documentclass{article}
\begin{document}
\begin{verbatim}foo\end{verbatim} This text will be NOT indented
\begin{verbatim}foo\end{verbatim}
This text will be NOT indented
\begin{verbatim}foo\end{verbatim}
This text will be indented
\begin{verbatim}foo\end{verbatim}\par This text will be indented
\end{document}