2

An answer to my last question gave a solution for hooking a small command onto line breaks in the TeX source.

\documentclass{memoir}
\usepackage[spacing, tracking]{microtype}
\usepackage{libertine}
\usepackage{xcolor}
\usepackage{xspace}

\nonfrenchspacing
\microtypecontext{spacing=nonfrench}

\newcounter{srcline}
\newcommand{\linemark}{\stepcounter{srcline}%
    \textcolor{red}{\textsc{Line~\thesrcline}}}

% Playing with the gobbling of \par
\makeatletter
{\obeylines\gdef\srclinepar{\@ifnextchar{^^M}{\endgraf\linemark\expandafter\srclinepar\@gobble}{ \linemark\xspace}}}
\makeatother

\newenvironment{srclines}
{\endgraf\let\par\srclinepar\obeylines}
{\endgraf}

\begin{document}
    \begin{srclines}
        Lorem ipsum dolor sit amet, consectetur adipiscing
        elit. Quisque porttitor ullamcorper velit, eu
        viverra augue varius in. Integer vehicula lacus
        vitae nunc blandit, sit amet auctor ex volutpat.
        Fusce ultrices elementum est, vel consectetur
        nulla elementum vel. Praesent in elit eu odio
        elementum luctus vel vitae leo. Sed vitae gravida

        nunc. Sed id posuere ligula. Donec a odio
        tincidunt, placerat nisl eget, egestas enim.
        Vivamus eget massa in quam fermentum cursus. In
        dictum, odio in laoreet fringilla, diam purus
        suscipit lorem, sit amet tempus ex lectus eget
        enim. Suspendisse potenti.%
    \end{srclines}
\end{document}

I am now trying to do something more complicated. Instead of expanding to a little bit of text, at every source line break I want to include a couple macros stored in another file, e.g. using \input{file.stuff} (I tested it using an empty file in the same directory).

\documentclass{memoir}
\usepackage[spacing, tracking]{microtype}
\usepackage{libertine}
\usepackage{xcolor}
\usepackage{xspace}

\nonfrenchspacing
\microtypecontext{spacing=nonfrench}

\newcounter{srcline}
\newcommand{\linemark}{\stepcounter{srcline}%
    \textcolor{red}{\textsc{Line~\thesrcline}}}

% Playing with the gobbling of \par
\makeatletter
{\obeylines\gdef\srclinepar{\@ifnextchar{^^M}{\endgraf\linemark\expandafter\srclinepar\@gobble}{ \linemark\xspace}\input{file.stuff}}}
% ^<----------------- here it is --------------
\makeatother

\newenvironment{srclines}
{\endgraf\let\par\srclinepar\obeylines}
{\endgraf}

\begin{document}
    \begin{srclines}
        Lorem ipsum dolor sit amet, consectetur adipiscing
        elit. Quisque porttitor ullamcorper velit, eu
        viverra augue varius in. Integer vehicula lacus
        vitae nunc blandit, sit amet auctor ex volutpat.
        Fusce ultrices elementum est, vel consectetur
        nulla elementum vel. Praesent in elit eu odio
        elementum luctus vel vitae leo. Sed vitae gravida

        nunc. Sed id posuere ligula. Donec a odio
        tincidunt, placerat nisl eget, egestas enim.
        Vivamus eget massa in quam fermentum cursus. In
        dictum, odio in laoreet fringilla, diam purus
        suscipit lorem, sit amet tempus ex lectus eget
        enim. Suspendisse potenti.%
    \end{srclines}
\end{document}

This, however, produces an error:

TeX capacity exceeded, sorry [text input levels=15]. ^^M

Is there a way to input an external file at every line-break hook?

  • At first sight you wrote \input{..} in the wrong place. You have to add it inside \linemark, or inside the true or false branches of \@ifnextchar (that is, second and third argument), but not after the third. – Manuel Jun 24 '15 at 0:17
2

Like this? You added \input at the wrong place, inside \linemark is where it should be.

\documentclass{memoir}
\usepackage[spacing, tracking]{microtype}
\usepackage{libertine}
\usepackage{xcolor}
\usepackage{xspace}

\nonfrenchspacing
\microtypecontext{spacing=nonfrench}

\newcounter{srcline}
\newcommand{\linemark}{\stepcounter{srcline}%
    \textcolor{red}{\textsc{Line~\thesrcline}} \input{file}}

% Playing with the gobbling of \par
\makeatletter
{\obeylines\gdef\srclinepar{\@ifnextchar{^^M}{\endgraf\linemark\expandafter\srclinepar\@gobble}{ \linemark\xspace}}}
% ^<----------------- here it is --------------
\makeatother

\begin{filecontents*}{file.tex}
  \textcolor{teal}{\bfseries!?}%
\end{filecontents*}

\newenvironment{srclines}
{\endgraf\let\par\srclinepar\obeylines}
{\endgraf}

\begin{document}
    \begin{srclines}
        Lorem ipsum dolor sit amet, consectetur adipiscing
        elit. Quisque porttitor ullamcorper velit, eu
        viverra augue varius in. Integer vehicula lacus
        vitae nunc blandit, sit amet auctor ex volutpat.
        Fusce ultrices elementum est, vel consectetur
        nulla elementum vel. Praesent in elit eu odio
        elementum luctus vel vitae leo. Sed vitae gravida

        nunc. Sed id posuere ligula. Donec a odio
        tincidunt, placerat nisl eget, egestas enim.
        Vivamus eget massa in quam fermentum cursus. In
        dictum, odio in laoreet fringilla, diam purus
        suscipit lorem, sit amet tempus ex lectus eget
        enim. Suspendisse potenti.%
    \end{srclines}
\end{document}

The filecontents environment is just to made the example self-contained.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.