1

I am just experimenting with a couple (about 50 or more) of simple text files, inputting them into a collection to be printed. My first problem, getting the first line printed as a heading in bold, was solved with the help of the accepted answer to another question (https://tex.stackexchange.com/a/385940/159816), which I simplified for my needs.

But I could find no way to get this same line into the toc, which I would like to put into the PDF with the \tableofcontents macro. If the listings package doesn't have the "hook" for that, maybe there is another possibility. I usually use LuaLaTeX, by the way.

Here is my code so far, with two example textfiles.

\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}

H.K.
  • 37

1 Answers1

1

I'm not sure if this can be done with a listings hook (it probably can), but a simple solution might be to read the first line with regular LaTeX commands and read the whole file again afterwards with listings.

This only works when the files do not contain syntax that might break LaTeX (like unbalanced braces or underscores outside of math mode) and anything that is valid LaTeX will be interpreted as such, i.e., the text is not processed verbatim like the result of \lstinputlisting. However, since your input is 'simple text files' it may be sufficient for your purposes.

MWE:

\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}

Result:

enter image description here

Marijn
  • 37,699