3

Here is my code:

\documentclass{article}
\usepackage{listings}
\begin{document}
Foo
{ \texttt \input{bar.txt} }
Baz
\end{document}

This produces the output:

Foo bar.txt Baz

I was expecting this output instead:

Foo <content of bar.txt> Baz

where <content of bar.txt> contains the actual content of the file named bar.txt.

I understand that my code is incorrect and I know it can be fixed like this:

\texttt{ \input{bar.txt} }

or like this:

{\ttfamily \input{bar.txt}}

But I want to know why my incorrect code ended up producing the literal text bar.txt in the output instead of the content of the file bar.txt?

  • 2
    You probably want {\ttfamily\input{bar.txt}} – egreg May 25 '18 at 19:07
  • Just do \meaning\texttt to see where that argument ends up (which in your first case is just the same as \texttt{\input}). – Manuel May 25 '18 at 19:08
  • @Manuel When I change the code to { \meaning\texttt \input{bar.txt} }, I get this in the output: Foo macro:-¿“protect “texttt Bar Baz. – Lone Learner May 25 '18 at 19:09
  • @Manuel If my code was interpreted as \texttt{\input} why do I not see the text \input appearing literally in the output? – Lone Learner May 25 '18 at 19:11
  • @LoneLearner Sorry, but it doesn't work that easy, so forget it. I meant to show you a method to check yourself what happens and why things end up where they are. – Manuel May 25 '18 at 19:16
8

the braces around a TeX macro argument may always be omitted if the argument is a single token. \fbox{a} may be written \fbox a

so

\texttt \input{bar.txt}

is

\texttt{\input}{\bar.txt}

which is (modulo some checking not relevant here)

{\ttfamily \input}{bar.txt}

so a group starts, the font is switched then TeX starts to process \input it then inputs the file .tex which does nothing other than make a warning message that it does nothing. On the terminal you see

(/home/davidc/texmf/tex/latex/tools/.tex File ignored)

Then the } closes the group so no text is set in tt font. Finally the {} around bar.txt are taken as a group, not argument delimiters, and bar.txt is typeset in the document font.

|improve this answer|||||

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.