1

I am making slides for my students, and I use the listings package

I want to display warning and error messages given when compiling basic erroneous C code.

A typical error I have trouble displying is to forget to close a string delimiter. The warning you get is

Errors1.c:5:16: warning: missing terminating '"' character [-Winvalid-pp-token]
    printf("the value of x is %i,x);

The problem when I write in in LateX using listings package is that it sees the first " as the beginning of a string delimiter, so everything that follows is formatted as a string.

See below a MWE. Everything after the " is interpreted as a string (visible spaces and italic). (Sorry I don't know how to display compiled latex here)

\documentclass[12pt]{article}
\usepackage{listings}
\lstdefinestyle{bash}{
language=bash,
breaklines=true, 
basicstyle=\ttfamily,
stringstyle=\textit,
morekeywords={warning,error},
showspaces=false,
}

\begin{document}
\begin{lstlisting}[style=bash]
Errors1.c:5:16: warning: missing terminating '"' character [-Winvalid-pp-token]
        printf("the value of x is %i,x);
               ^
Errors1.c:5:16: error: expected expression
\end{lstlisting}
\end{document}

Thanks for helping me

Pierre

  • Welcome to TeX - LaTeX. To display a result, you can take a screenshot of your compiled document and embed it into your post. – LokiRagnarok Sep 9 '19 at 11:00
  • To me there is something strange about your choice of language here. You are not displaying something from the bash language, but an output into the bash shell. I'd probably just ignore bash as a language here as this is not displaying the code for a bash script. – daleif Sep 9 '19 at 11:02
1

To answer the question as asked: you can ignore string delimiters using the deletestring style option. This option should reflect the original definition of the delimiter, which for bash and sh includes the [d] escape mode, i.e., a literal " within a string can be escaped by [d]oubling it. Escaping the character within a string does not help you here, because you are not in a string, but you need to know it in order to properly remove the delimiter from the language definition.

However, if you are not using syntax highlighting then you can also use a verbatim environment. When the input contains long lines then you can use the Verbatim environment (note the upper case V) with the breaklines option from the package fvextra/fancyvrb.

MWE:

\documentclass[12pt]{article}
\usepackage{listings}
\usepackage{fvextra}
\lstdefinestyle{bash}{
language=bash,
breaklines=true, 
basicstyle=\ttfamily,
stringstyle=\textit,
morekeywords={warning,error},
showspaces=false,
deletestring=[d]"
}

\begin{document}
\begin{lstlisting}[style=bash]
Errors1.c:5:16: warning: missing terminating '"' character [-Winvalid-pp-token]
        printf("the value of x is %i,x);
               ^
Errors1.c:5:16: error: expected expression
\end{lstlisting}

\begin{Verbatim}[breaklines=true]
Errors1.c:5:16: warning: missing terminating '"' character [-Winvalid-pp-token]
        printf("the value of x is %i,x);
               ^
Errors1.c:5:16: error: expected expression
\end{Verbatim}

\end{document}

Result:

enter image description here

|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.