3
\documentclass[titlepage]{article}
\author{foobar}
\title{foofoofoo}
\usepackage{syntonly}
\usepackage{verbatim}
\usepackage{lipsum}
\usepackage{listings}
\begin{document}
\maketitle
\newpage
\tableofcontents
\newpage
\lstset{basicstyle=\texttt} %THIS LINE
\section{Source Code}
I will admit that this code is {\it messy}.
\subsection{foo.c}
\lstinputlisting{foo.c}
\end{document}

I don't understand why that line makes pdflatex not compile; when I remove it, everything works fine.

It gives me this:

! Incomplete \ifmmode; all text was ignored after line 
1
  • 2
    Welcome to TeX.SX! Replace the \texttt by \ttfamily and \it should be either \itshape messy or \textit{messy}
    – user31729
    Jan 8, 2016 at 19:27

1 Answer 1

6

Since \texttt is a command that expects an argument, it won't find strange characters etc. in listings verbatim mode. It's not possible to give verbatim code as an argument to another command. This is why \texttt{...} must fail.

In order to achieve typewriter font, use \ttfamily to switch to this font. Note, this is an enduring switch (until another switch is applied), whereas \texttt{...} lasts only for the {...} - delimited argument.

\documentclass[titlepage]{article}
\author{foobar}
\title{foofoofoo}
\usepackage{syntonly}
\usepackage{verbatim}
\usepackage{lipsum}
\usepackage{listings}
\begin{document}
\maketitle
\newpage
\tableofcontents
\newpage
\lstset{basicstyle=\ttfamily} %THIS LINE
\section{Source Code}
I will admit that this code is {\textit{messy}}.
\subsection{foo.c}
\lstinputlisting{foo.c}
\end{document}

foo.c:

#include <stdio.h>

int main( int argc, char **argv)
{
  printf("Hello World!\n");
  return(0);
}

enter image description here

2
  • Thank you! Why does texttt not work? Jan 8, 2016 at 19:30
  • @occamsrazor: I explained and extended it on the top of my answer
    – user31729
    Jan 8, 2016 at 19:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .