命令错误中的列表:输入行上列表开始后文本被删除

命令错误中的列表:输入行上列表开始后文本被删除

当我想测试一些对齐选项以询问有关的另一个问题时listings,我遇到了一个我没有预料到的问题。

考虑以下代码:

% Preamble
\documentclass[letterpaper, 10pt, onecolumn]{article}

% Packages
\usepackage{listings}

% Lst options
\lstset{
    language = C++,
    frame = lines,
    framesep = 0pt,
    rulesep = 0pt,
    aboveskip = 0pt,
    belowskip = 0pt,
}

% Commands
\newcommand{\alphabet}{ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz}
\newcommand{\makelisting}[1]{
\newpage\noindent\alphabet
\begin{lstlisting}[basicstyle = \fontfamily{cmtt}\selectfont#1]
int main(int argc, char* argv[])
{
    return 0;
}
\end{lstlisting}
\alphabet
}

% Document
\begin{document}
\makelisting{\tiny}
\makelisting{\scriptsize}
\makelisting{\footnotesize}
\makelisting{\small}
\makelisting{\normalsize}
\makelisting{\large}
\makelisting{\Large}
\makelisting{\LARGE}
\makelisting{\huge}
\makelisting{\Huge}
\end{document}

这应该很简单。但是,我最终得到了以下错误:

Package Listings Warning: Text dropped after begin of listing on input line 32.

并且没有生成任何文件。这是怎么回事?如何解决?

答案1

除了将示例代码保存在文件中,我认为没有其他办法。

\begin{filecontents*}{\jobname-sample.c}
int main(int argc, char* argv[])
{
    return 0;
}
\end{filecontents*}

\documentclass{article}

% Packages
\usepackage{listings}

% Lst options
\lstset{
    language = C++,
    frame = lines,
    framesep = 0pt,
    rulesep = 0pt,
    aboveskip = 0pt,
    belowskip = 0pt,
}

% Commands
\newcommand{\alphabet}{ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz}
\newcommand{\makelisting}[1]{%
  \bigskip\noindent\alphabet
  \lstinputlisting[basicstyle = \fontfamily{cmtt}\selectfont#1]{\jobname-sample.c}
  \alphabet\par
}

% Document
\begin{document}
\makelisting{\tiny}
\makelisting{\scriptsize}
\makelisting{\footnotesize}
\makelisting{\small}
\makelisting{\normalsize}
\makelisting{\large}
\makelisting{\Large}
\makelisting{\LARGE}
\makelisting{\huge}
%\makelisting{\Huge}
\end{document}

在此处输入图片描述

答案2

\makelisting您可以做的是让 LaTeX 读取并标记逐字类别代码制度下的代码列表,然后将其传递到包装到调用的定义中\scantokens

为了让 LaTeX 读取和标记逐字类别代码制度下的内容,我经常使用\UDcollectverbarg具有以下语法的例程:

\UDcollectverbarg{⟨^^M-replacement⟩}{⟨Mandatory 1⟩}{⟨Mandatory 2⟩}⟨verbatimized argument⟩

得出:

⟨Mandatory 1⟩{⟨Mandatory 2⟩{⟨verbatimized argument⟩}}

,其中表示行尾的每个字符^^M都被标记序列替换⟨^^M-replacement⟩

参数⟨Mandatory 1⟩⟨Mandatory 2⟩是必需的。如果它们由多个标记组成,则必须将它们嵌套到 catcode-1/2 字符对 / 括号中。
如果需要读取和标记化以获取其中一些,则这将在未更改的类别代码制度下进行。
也是⟨verbatimized argument⟩必需的。它将从 .tex-input 文件中读取并标记根据逐字分类代码制度。如果其第一个字符是括号,则将“假定”该参数嵌套在括号中。否则,将假定该参数的结尾由第一个字符界定 - 就像的参数一样\verb
空行不会被忽略。

我选择这种语法是因为通过这种语法,您可以⟨Mandatory 2⟩通过嵌套调用 -argument 中的 -argument 来收集-argument\UDcollectverbarg中的逐字参数。\UDcollectverbarg⟨Mandatory 1⟩

例如,

\UDcollectverbarg{⟨^^M-replacement⟩}%
                 {\UDcollectverbarg{⟨^^M-replacement⟩}{\UDcollectverbarg{⟨^^M-replacement⟩}{⟨actionA⟩}}}%  <- Mandatory 1
                 {⟨actionB⟩}%                     <- Mandatory 2
                 ⟨verbatimized argument 1⟩⟨verbatimized argument 2⟩⟨verbatimized argument 3⟩

产量:

\UDcollectverbarg{⟨^^M-replacement⟩}{\UDcollectverbarg{⟨^^M-replacement⟩}{⟨actionA⟩}}% <- Mandatory 1
                 {⟨actionB⟩{⟨verbatimized argument 1⟩}}%        <- Mandatory 2
                 ⟨verbatimized argument 2⟩⟨verbatimized argument 3⟩

产量:

\UDcollectverbarg{⟨^^M-replacement⟩}{⟨actionA⟩}% <- Mandatory 1
                 {⟨actionB⟩{⟨verbatimized argument 1⟩}{⟨verbatimized argument 2⟩}}% <- Mandatory 2
                 ⟨verbatimized argument 3⟩

产量:

⟨actionA⟩{⟨actionB⟩{⟨verbatimized argument 1⟩}{⟨verbatimized argument 2⟩}{⟨verbatimized argument 3⟩}}

假设⟨actionA⟩= \@firstofone

\@firstofone{⟨actionB⟩{⟨verbatimized argument 1⟩}{⟨verbatimized argument 2⟩}{⟨verbatimized argument 3⟩}}

产量:

⟨actionB⟩{⟨verbatimized argument 1⟩}{⟨verbatimized argument 2⟩}{⟨verbatimized argument 3⟩}

以下是示例:

% Preamble
\documentclass[letterpaper, 10pt, onecolumn]{article}

% Adjust horizontal margins so that listings in \Huge fits on paper also:
\makeatletter
\setlength\evensidemargin{2.2cm}%
\setlength\marginparsep{.25\evensidemargin}%
\setlength\marginparwidth{.5\evensidemargin}%
\setlength\oddsidemargin{\[email protected]\fi\evensidemargin}%
\setlength\textwidth\paperwidth
\addtolength\textwidth{-\[email protected]\else2\fi\evensidemargin}%
\addtolength\evensidemargin{-1in}%
\addtolength\oddsidemargin{-1in}%
\@ifundefined{pdfpagewidth}{}{\setlength\pdfpagewidth{\paperwidth}}%
\@ifundefined{pdfpageheight}{}{\setlength\pdfpageheight{\paperheight}}%
\@ifundefined{pagewidth}{}{\setlength\pagewidth{\paperwidth}}%
\@ifundefined{pageheight}{}{\setlength\pageheight{\paperheight}}%
\makeatother

% Packages
\usepackage{listings}

% Lst options
\lstset{
    language = C++,
    frame = lines,
    framesep = 0pt,
    rulesep = 0pt,
    aboveskip = 0pt,
    belowskip = 0pt,
}

% Commands
\newcommand{\alphabet}{ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz}

%%<..................Paraphernalia.....................................>
\makeatletter
\newcommand\UD@firstofone[1]{#1}%
\newcommand\UD@firstoftwo[2]{#1}%
\newcommand\UD@secondoftwo[2]{#2}%
%%<-------------------- Code for \UDcollectverbarg -------------------->
%% Check whether argument is empty:
%%......................................................................
%% \UD@CheckWhetherNull{<Argument which is to be checked>}%
%%                     {<Tokens to be delivered in case that argument
%%                       which is to be checked is empty>}%
%%                     {<Tokens to be delivered in case that argument
%%                       which is to be checked is not empty>}%
%% The gist of this macro comes from Robert R. Schneck's \ifempty-macro:
%% <https://groups.google.com/forum/#!original/comp.text.tex/kuOEIQIrElc/lUg37FmhA74J>
\newcommand\UD@CheckWhetherNull[1]{%
  \romannumeral0\expandafter\UD@secondoftwo\string{\expandafter
  \UD@secondoftwo\expandafter{\expandafter{\string#1}\expandafter
  \UD@secondoftwo\string}\expandafter\UD@firstoftwo\expandafter{\expandafter
  \UD@secondoftwo\string}\expandafter\expandafter\UD@firstoftwo{ }{}%
  \UD@secondoftwo}{\expandafter\expandafter\UD@firstoftwo{ }{}\UD@firstoftwo}%
}%
%%......................................................................
\begingroup
\catcode`\^^M=12 %
\UD@firstofone{%
  \endgroup%
  \newcommand\UDEndlreplace[2]{\romannumeral0\@UDEndlreplace{#2}#1^^M\relax{}}%
  \newcommand*\@UDEndlreplace{}%
  \long\def\@UDEndlreplace#1#2^^M#3\relax#4#5{%
    \UD@CheckWhetherNull{#3}%
    { #5{#4#2}}{\@UDEndlreplace{#1}#3\relax{#4#2#1}{#5}}%
  }%
}%
\newcommand\UDcollectverbarg[3]{%
  \begingroup
  \let\do\@makeother % <- this and the next line switch to
  \dospecials        %    verbatim-category-code-régime.
  \catcode`\{=1      % <- give opening curly brace the usual catcode so a 
                     %    curly-brace-balanced argument can be gathered in
                     %    case of the first thing of the verbatimized-argument 
                     %    being a curly opening brace.
  \catcode`\ =10     % <- give space and horizontal tab the usual catcode so \UD@collectverbarg
  \catcode`\^^I=10   %    cannot catch a space or a horizontal tab as its 4th undelimited argument.
                     %    (Its 4th undelimited argument denotes the verbatim-
                     %     syntax-delimiter in case of not gathering a
                     %     curly-brace-nested argument.)
  \kernel@ifnextchar\bgroup
  {% seems a curly-brace-nested argument is to be caught:
    \catcode`\}=2    % <- give closing curly brace the usual catcode also.
    \UD@collectverbarg{#1}{#2}{#3}{}%
  }{% seems an argument with verbatim-syntax-delimiter is to be caught:
    \do\{% <- give opening curly brace the verbatim-catcode again.
    \UD@collectverbarg{#1}{#2}{#3}%
  }%
}%
\newcommand\UD@collectverbarg[4]{%
  \do\ %   <- Now that \UD@collectverbarg has the delimiter or
  \do\^^I%    emptiness in its 4th arg, give space and horizontal tab
         %    the verbatim-catcode again.
  \do\^^M% <- Give the carriage-return-character the verbatim-catcode.
  \long\def\@tempb##1#4{%
    %\edef\@tempb{##1}%
    \def\@tempb{##1}%
    \@onelevel@sanitize\@tempb % <- Turn characters into their "12/other"-pendants.
                               %    This may be important with things like the 
                               %    inputenc-package which may make characters 
                               %    active/which give them catcode 13(active).
    \expandafter\UDEndlreplace\expandafter{\@tempb}{#1}{\def\@tempb}% <- this starts 
                               %    the loop for replacing endline-characters.
    \expandafter\UD@@collectverbarg\expandafter{\@tempb}{#2}{#3}% <- this "spits 
                               %    out the result.
  }%
  \@tempb
}%
\newcommand\UD@@collectverbarg[3]{%
  \endgroup
  #2{#3{#1}}%
}%
\makeatother
%%<---------------- End of code for \UDcollectverbarg ----------------->

\makeatletter
\begingroup
\newcommand\makelisting[2]{%
  \endgroup
  \newcommand\makelisting[1]{%
    \newpage
    \noindent\alphabet
    \scantokens{%
      \par\noindent #1##1#2%
    }%
    \alphabet
  }%
}%
\UDcollectverbarg{^^J}%
                 {%
                   \UDcollectverbarg{^^J}%
                                     {\UD@firstofone}%
                 }%
                 {\makelisting}|\begin{lstlisting}[basicstyle = \fontfamily{cmtt}\selectfont||]
int main(int argc, char* argv[])
{
    return 0;
}
\end{lstlisting}
%|

\makeatother

%\show\makelisting

\begin{document}
\makelisting{\tiny}
\makelisting{\scriptsize}
\makelisting{\footnotesize}
\makelisting{\small}
\makelisting{\normalsize}
\makelisting{\large}
\makelisting{\Large}
\makelisting{\LARGE}
\makelisting{\huge}
\makelisting{\Huge}
\end{document}

在此处输入图片描述 在此处输入图片描述 在此处输入图片描述 在此处输入图片描述 在此处输入图片描述 在此处输入图片描述 在此处输入图片描述 在此处输入图片描述 在此处输入图片描述 在此处输入图片描述

相关内容