4

I have a problem with LateX. I'm using the command \lstinputlisting to insert my C source code in my LateX document, and I commented my code using the LateX syntax to insert predicates (I need to specify Hoare triples throughout my code).

Here's the problematic code section:

for(i=0; i<fact(n); i++){ //INV: $ 0 \leq i < n! $
        for(j=0; j<n; j++){ //INV2: $ 0 \leq j < n $

I get both a "Missing } inserted" and a "Missing $ inserted" error with the second line, but not with the first line (so I guess the first one is correct). I really don't understand why, because they're almost identical so if the first one works, the second one should, right?

I tried several things:

//$ INV2: 0 \leq j < n $
//$ Inv_{2}: 0 \leq j < n $
//$Inv_{2}$ : $ 0 \leq j < n $

Nothing seems to work.

  • Can you share some of the latex code? I can't really reproduce this using \lstinputlisting{src.c} (src.c contains the 2 for loops with the comments). – apokryfos Apr 8 '16 at 15:11
  • @purplepsycho I read a few posts like this one but I can't find any underscore (or any other special symbol) that could cause the error on the second line, hence my question here. Thanks for the link though. – Yngvi Apr 8 '16 at 15:20
  • @apokryfos Well in my Latex code I just use \lstinputlisting{...}, so the error must come from my source code and not the latex code (which is pretty standard and doesn't give me any error if I remove \lstinputlisting) – Yngvi Apr 8 '16 at 15:23
  • Still not sure I understand way you're using \lstinputlisting I'll post a my code which compiles Ok and let me know if it's what you need. – apokryfos Apr 8 '16 at 15:49
1

This is the LateX code I've written:

\documentclass[a4paper,10pt]{article}

\usepackage{listings}

\begin{document}

\lstset{ %
  texcl=true,
  escapeinside={//}{\^^M},
}


\lstinputlisting{src.c}

\end{document}

src.c

for(i=0; i<fact(n); i++){ //INV: $ 0 \leq i < n! $  
        for(j=0; j<n; j++){ //INV2: $ 0 \leq j < n $ 

Notice that I've used escape characters to format LateX in the source along with the escapeinside option.

Inline version of the code:

\documentclass[a4paper,10pt]{article}

\usepackage{listings}

\begin{document}

 \lstset{ %
   texcl=true,
   escapeinside={//}{\^^M},
}

\begin{lstlisting}
  for(i=0; i<fact(n); i++){ //INV: $ 0 \leq i < n! $  
        for(j=0; j<n; j++){ //INV2: $ 0 \leq j < n $   
\end{lstlisting}


\end{document}
  • I tried using that, but I still get an error on a line and now it even shows me a weird error stating that I forgot a $ or put an extra } in three different lines of my lstset ( numbers=left, numbersep=5pt and numberstyle=\tiny\color{mygray}), which doesn't make sense because I didn't have these errors before. In lstset I put "texcl=true", which interprets line comments (after //) as Latex code, and on a previous project it worked perfectly, I didn't need to use %* and *). – Yngvi Apr 8 '16 at 16:21
  • Sounds like your problem is before this environment and not this one itself. Anyway I've updated it with textcl=true and escapeinside={//}{\^^M} (because textcl on its own wasn't doing it for me). Maybe it can help someone else. – apokryfos Apr 8 '16 at 16:32

Your Answer

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