如何在 Verbatim 环境中用行号对齐文本?

如何在 Verbatim 环境中用行号对齐文本?

我的最小文件是:

\documentclass{article}
\usepackage{fancyvrb}
\usepackage{ragged2e}
\usepackage{lipsum}
\usepackage{pbox}
\begin{document}
\begin{Verbatim}[numbers=left,fontsize=\bf,commandchars=\^\#\*,baselinestretch=1,firstnumber=1,stepnumber=1]
    \begin{document}
    ^justifying^pbox#12cm*#The flowers of most species have five petals, with the exception of Rosa sericea, which usually has only four. Each petal is divided into two distinct lobes and is usually white or pink, though in a few species yellow or red. Beneath the petals are five sepals (or in the case of some Rosa sericea, four). These may be long enough to be visible when viewed from above and appear as green points alternating with the rounded petals. There are multiple superior ovaries that develop into achenes.[4] Roses are insect-pollinated in nature.*
\end{document}
\end{Verbatim}
\end{document}

但是right side我的textalign正确。我的line number也不正确。而且我还有以下警告:

Underfull \hbox (badness 10000) in paragraph

在此处输入图片描述

答案1

基于https://tex.stackexchange.com/a/799/36296

\documentclass{article}
\usepackage{fancyvrb}
%\usepackage{ragged2e}
\usepackage{lipsum}
\usepackage{pbox}

\newcommand*\justify{%
  \fontdimen2\font=0.4em% interword space
  \fontdimen3\font=0.2em% interword stretch
  \fontdimen4\font=0.1em% interword shrink
  \fontdimen7\font=0.1em% extra space
  \hyphenchar\font=`\-% allowing hyphenation
}


\begin{document}
\begin{Verbatim}[numbers=left,fontsize=\bf,commandchars=\^\#\*,baselinestretch=1,firstnumber=1,stepnumber=1]
\begin{document}
^justify^pbox#12cm*#The flowers of most species have five petals, with the exception of Rosa sericea, which usually has only four. Each petal is divided into two distinct lobes and is usually white or pink, though in a few species yellow or red. Beneath the petals are five sepals (or in the case of some Rosa sericea, four). These may be long enough to be visible when viewed from above and appear as green points alternating with the rounded petals. There are multiple superior ovaries that develop into achenes.[4] Roses are insect-pollinated in nature.*
\end{document}
\end{Verbatim}
\end{document}

在此处输入图片描述


也许有一个简单的替代方法是对每行进行编号:

\documentclass{article}

\newcommand*\justify{%
  \fontdimen2\font=0.4em% interword space
  \fontdimen3\font=0.2em% interword stretch
  \fontdimen4\font=0.1em% interword shrink
  \fontdimen7\font=0.1em% extra space
  \hyphenchar\font=`\-% allowing hyphenation
}

\usepackage{lineno}

\begin{document}

\begin{linenumbers}
\ttfamily
\setlength{\parindent}{0pt}
\justify

\verb|\begin{document}|

The flowers of most species have five petals, with the exception of Rosa sericea, which usually has only four. Each petal is divided into two distinct lobes and is usually white or pink, though in a few species yellow or red. Beneath the petals are five sepals (or in the case of some Rosa sericea, four). These may be long enough to be visible when viewed from above and appear as green points alternating with the rounded petals. There are multiple superior ovaries that develop into achenes.[4] Roses are insect-pollinated in nature.

\verb|\end{document}|
\end{linenumbers}

\end{document}

在此处输入图片描述

答案2

存在三个问题:行号在段落中间、段落不对齐、段落与行之间的跳行\begin{document}太小\end{document}

第一个问题可以通过可选参数[t]来解决\pbox,该参数将的基线设置\pbox为其第一行。

\strut第三个问题可以通过在段落的开头和结尾添加内容来解决。

对于第二个问题,证明段落的合理性,这个答案(与 samcarters 的回答相同)是必需的。但这会全局设置字体大小,这可能不是我想要的。因此我编写了宏来存储、设置和恢复它们。为了避免每次都要输入所有这些,还有一个\justifiedtt语法与\pbox(内部使用)相同的宏。

以下是解决方案不同部分的结果(我只是稍微缩短了文本):

在此处输入图片描述

代码如下:

\documentclass{article}
\usepackage{fancyvrb}
\usepackage{ragged2e}
\usepackage{lipsum}
\usepackage{pbox}

\makeatletter
\newdimen\@orig@fdiii
\newdimen\@orig@fdiv
\newcount\@orig@hych
\newcommand*\@set@interword{%
  \@orig@fdiii=\fontdimen3\font
  \@orig@fdiv=\fontdimen4\font
  \@orig@hych=\hyphenchar\font
  \fontdimen3\font=0.2em% interword stretch
  \fontdimen4\font=0.1em% interword shrink
  \hyphenchar\font=`\-% allowing hyphenation
}
\newcommand*\@restore@interword{%
  \fontdimen3\font=\@orig@fdiii
  \fontdimen4\font=\@orig@fdiv
  \hyphenchar\font=\@orig@hych
}

\newcommand{\justifiedtt}[3][c]{\pbox[#1]{#2}{\ttfamily\@set@interword\strut#3\strut\@restore@interword}}
\makeatother

\begin{document}
\noindent
Original:
\begin{Verbatim}[numbers=left,fontsize=\bf,commandchars=\^\#\*,baselinestretch=1,firstnumber=1,stepnumber=1]
\begin{document}
^pbox#^textwidth*#The flowers of most species have five petals, with the exception of Rosa sericea, which usually has only four. Each petal is divided into two distinct lobes and is usually white or pink, though in a few species yellow or red.*
\end{document}
\end{Verbatim}

\noindent
With \verb|[t]|:
\begin{Verbatim}[numbers=left,fontsize=\bf,commandchars=\^\#\*,baselinestretch=1,firstnumber=1,stepnumber=1]
\begin{document}
^pbox[t]#^textwidth*#The flowers of most species have five petals, with the exception of Rosa sericea, which usually has only four. Each petal is divided into two distinct lobes and is usually white or pink, though in a few species yellow or red.*
\end{document}
\end{Verbatim}

\noindent
With \verb|\strut| at start and end:
\begin{Verbatim}[numbers=left,fontsize=\bf,commandchars=\^\#\*,baselinestretch=1,firstnumber=1,stepnumber=1]
\begin{document}
^pbox#^textwidth*#^strut#*The flowers of most species have five petals, with the exception of Rosa sericea, which usually has only four. Each petal is divided into two distinct lobes and is usually white or pink, though in a few species yellow or red.^strut*
\end{document}
\end{Verbatim}

\noindent
With \verb|\justifiedtt|:
\begin{Verbatim}[numbers=left,fontsize=\bf,commandchars=\^\#\*,baselinestretch=1,firstnumber=1,stepnumber=1]
\begin{document}
^justifiedtt[t]#^textwidth*#The flowers of most species have five petals, with the exception of Rosa sericea, which usually has only four. Each petal is divided into two distinct lobes and is usually white or pink, though in a few species yellow or red.*
\end{document}
\end{Verbatim}

\noindent
\verb|\justifiedtt| outside \verb|Verbatim| also works:

\noindent
\justifiedtt[t]{\textwidth}{The flowers of most species have five petals, with the exception of Rosa sericea, which usually has only four. Each petal is divided into two distinct lobes and is usually white or pink, though in a few species yellow or red.}

\end{document}

相关内容