LaTeX forum ⇒ Text FormattingCaption name issue using listings package Topic is solved

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Singularity
Posts: 155
Joined: Sat Jan 22, 2011 9:55 pm

Caption name issue using listings package

Postby Singularity » Sun Mar 23, 2014 5:14 am

The listings package won't load only some of my program files. The first file in the following MWE loads no problem, but the second one returns errors about inserting missing "$" and missing "}" and then about how it had extra or forgotten "$" and "}". Why doesn't it just not put in the extra "$" and "}"?

The issue seems to be the difference in caption names and, more specifically, that the second caption has a "2D" in it (or maybe because it has an underscore in it), which must look like math to TeXnicCenter.

How do I get it to recognize this as text? How do I get this working?

Thanks.

  1. \documentclass[]{article}
  2. \usepackage{listings}
  3. \begin{document}
  4.  
  5. \lstdefinestyle{mycode}{basicstyle=\small, % print whole listing small
  6. numbers=none,%left, numberstyle=\tiny, stepnumber=5, numbersep=5pt,
  7. frame=single,
  8. breaklines=true
  9. }
  10.  
  11. \lstset{
  12. style=mycode,
  13. language=Octave,
  14. caption={initFPU.m},
  15. label={code:initFPU.m}
  16. }
  17. \lstinputlisting{code/kruskal/initFPU.m}
  18.  
  19. \lstset{
  20. style=mycode,
  21. language=Octave,
  22. caption={initFPU_2D.m},
  23. label={code:initFPU_2D.m}
  24. }
  25. \lstinputlisting{code/kruskal/initFPU_2D.m}
  26. \end{document}

Note: you can't get this to work in writelatex without some files named initFPU.m and initFPU_2D.m.

Singularity
Posts: 155
Joined: Sat Jan 22, 2011 9:55 pm

Postby Singularity » Sun Mar 23, 2014 5:31 am

It seems I can fix it by putting a "\" before the "_". But it prints as a kind of short "_". If there's a better fix, I'm still interested.

hugovdberg
Posts: 133
Joined: Sat Feb 25, 2012 6:12 pm

Postby hugovdberg » Sun Mar 23, 2014 12:48 pm

The underscore is indeed an active character that is only allowed in mathmode. So to print a literal _ you need \_, which simply prints the underscore from the current font. In the default font this is a rather narrow character, to make a wider underscore you could something like this:

  1. \documentclass{article}
  2.  
  3. % change the letter inside the [LaTeX]\hphantom[/LaTeX] to a wider or narrower character to change the width of the underscore
  4. \newcommand{\wideunderscore}{\underline{\hphantom{n}}}
  5.  
  6. \begin{document}
  7. init\wideunderscore 2D
  8.  
  9. init\_2D
  10. \end{document}
Ubuntu 13.10 + Tex Live 2013 + Texmaker / Windows 7 Pro + MikTex 2.9 + TexnicCenter / Android 4.3 + TexPortal + DroidEdit


Return to “Text Formatting”

Who is online

Users browsing this forum: No registered users and 4 guests