如何将外部文本(通过 listing 包的 `\lstinputlisting` 输入)中的一行放入目录中?

如何将外部文本(通过 listing 包的 `\lstinputlisting` 输入)中的一行放入目录中?

我只是在尝试几个(大约 50 个或更多)简单的文本文件,将它们输入到要打印的集合中。我的第一个问题是将第一行打印为粗体标题,在另一个问题的可接受答案的帮助下解决了(https://tex.stackexchange.com/a/385940/159816),我根据自己的需要对其进行了简化。

但是我找不到将同一行放入目录中的方法,我想用宏将其放入 PDF 中\tableofcontents。如果 listings 包没有“钩子”,也许还有另一种可能性。顺便说一句,我通常使用 LuaLaTeX。

这是我迄今为止的代码,其中包含两个示例文本文件。

\documentclass{article}
\usepackage{forloop}
\usepackage{listings}

\newcounter{ct}
\pagestyle{empty}

\makeatletter
\lst@AddToHook{OutputBox}{\lst@boldline}
\let\lst@boldline\relax
\newcommand\lstboldline{%
    \def\lst@boldline{\ifnum\lst@lineno=1 \bfseries\fi}}
\makeatother

\begin{filecontents*}{1.txt}
First Line of First Text

Rest of First text Rest of First text Rest of First text Rest of First text Rest of First text

Rest of First text Rest of First text
Rest of First text
Rest of First text Rest of First text

Rest of First text Rest of First text Rest of First text
Rest of First text Rest of First text
\end{filecontents*}

\begin{filecontents*}{2.txt}
First Line of Second Text

Rest of Second text Rest of Second text Rest of Second text Rest of Second text Rest of Second text

Rest of Second text Rest of Second text
Rest of Second text

\end{filecontents*}

\begin{document}
\large
\forloop{ct}{1}{\value{ct} < 3}%
{%
\lstboldline
\lstinputlisting[basicstyle=\sffamily\slshape,
                 breaklines,
                 breakautoindent=false,
                 breakindent=0pt,
                 breakatwhitespace=true,
                 columns=fullflexible,
                 language=]{\arabic{ct}.txt}
}% End of the loop, go to next text
\end{document}

答案1

我不确定是否可以用listings钩子来完成此操作(可能可以),但一个简单的解决方案可能是使用常规 LaTeX 命令读取第一行,然后使用 再次读取整个文件listings

这仅在文件不包含可能破坏 LaTeX 的语法(如数学模式之外的不平衡括号或下划线)时才有效,并且任何有效的 LaTeX 内容都将被解释为有效,即文本不会像 的结果那样逐字处理\lstinputlisting。但是,由于您的输入是“简单文本文件”,它可能足以满足您的目的。

梅威瑟:

\documentclass{article}
\usepackage{forloop}
\usepackage{listings}

\newcounter{ct}
\pagestyle{empty}

\makeatletter
\lst@AddToHook{OutputBox}{\lst@boldline}
\let\lst@boldline\relax
\newcommand\lstboldline{%
    \def\lst@boldline{\ifnum\lst@lineno=1 \bfseries\fi}}
\makeatother

\begin{filecontents*}{1.txt}
First \textit{Line} of First Text

Rest of First text Rest of First text Rest of First text Rest of First text Rest of First text

Rest of First text Rest of First text
Rest of First text
Rest of First text Rest of First text

Rest of First text Rest of First text Rest of First text
Rest of First text Rest of First text
\end{filecontents*}

\begin{filecontents*}{2.txt}
First Line of Second Text

Rest of Second text Rest of Second text Rest of Second text Rest of Second text Rest of Second text

Rest of Second text Rest of Second text
Rest of Second text

\end{filecontents*}
% create file handle
\newread\mytempin

\begin{document}
\tableofcontents
\section{Text files}
\large
\forloop{ct}{1}{\value{ct} < 3}%
{%
% open the file, read the first line, add to TOC, close the file
% afterwards process as normal (including first line)
% with \lstinputlisting
\openin\mytempin=\arabic{ct}.txt %
\read\mytempin to \firstline
\addcontentsline{toc}{section}{\firstline}
\closein\mytempin
\lstboldline
\lstinputlisting[basicstyle=\sffamily\slshape,
                 breaklines,
                 breakautoindent=false,
                 breakindent=0pt,
                 breakatwhitespace=true,
                 columns=fullflexible,
                 language=]{\arabic{ct}.txt}
}% End of the loop, go to next text
\end{document}

结果:

在此处输入图片描述

相关内容