GeneralHash symbol breaking formating in lstlisting environment

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
loris
Posts: 1
Joined: Tue Jan 12, 2010 11:20 am

Hash symbol breaking formating in lstlisting environment

Post by loris »

Hi,

I am using the listing package to display a shell script in
a presentation using the beamer package.

Code: Select all

          \lstset{language=csh,showstringspaces=false,basicstyle=\footnotesize}
          \begin{lstlisting}
#!/bin/csh
if ( $#argv < 2 ) then
 echo "usage: $0 name age" 
 exit 1
endif
          \end{lstlisting}
However, the "#" seems to cause the formating to break - the rest of the
line is slanted and "then" is not bold. If I remove the "#" everything
looks fine. I couldn't find anything about this in the documentation.

Any ideas?

Thanks,

Loris

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

Re: Hash symbol breaking formating in lstlisting environment

Post by phi »

The listings package probably thinks that the # starts a comment here. I have no idea at the moment except for trying a different pretty-printer like Pygments.
rehoot
Posts: 3
Joined: Wed Jan 13, 2010 9:38 pm

Re: Hash symbol breaking formating in lstlisting environment

Post by rehoot »

The line is displaying in italics because the hash mark indicates that the line is a comment.

If you really want to redefine how the language is displayed, I know a way to do it but it takes some work. I tried putting the override in the document itself, but it seems that the listings.sty package wants the definition in a file, so here is what I did.

1) I wrote a new language definition by copying the csh style from texmf-dist/tex/latex/listings/lstlang1.sty. I put this definition in its own .sty file called lstlangbob.sty. If you have never done this, then there are extra instructions...
A) locate your TEXMFLOCAL directory by running:
texconfig conf
and then looking for for a line in the output that starts with "TEXMFLOCAL".
B) Go to the TEXMFLOCAL directory, then go into or create a subdirectory: tex/latex/local
C) in that tex/latex/local subdirectory, create a file called lstlanbob.sty (or whatever you want) and paste this into it:

\lst@definelanguage{cshX}
{morekeywords={alias,awk,cat,echo,else,end,endif,endsw,exec,exit,%
foreach,glob,goto,history,if,logout,nice,nohup,onintr,repeat,sed,%
set,setenv,shift,source,switch,then,time,while,umask,unalias,%
unset,wait,while,@,env,argv,child,home,ignoreeof,noclobber,%
noglob,nomatch,path,prompt,shell,status,verbose,print,printf,%
sqrt,BEGIN,END},%morecomment=[l],%
morestring=[d]"%
}[keywords,comments,strings]%

D) run:
sudo texhash

2) your document should look something like this if you want all occurrences of the csh listings to look the same. Otherwise you can switch between the csh style and the cshX style that I created.

\documentclass{beamer}
% latexBob.py lstlisting2 ~/Documents/LaTeX/Examples/
\usepackage{listings}

\def\lstlanguagefiles
{lstlang0.sty,lstlang1.sty,lstlang2.sty,lstlang3.sty,lstlangbob.sty}

\lstset{language=cshX,showstringspaces=false,basicstyle=\footnotesize}

\begin{document}
\begin{frame}[fragile]
here is a listing of a shell script:
\begin{lstlisting}
#!/bin/csh
if ( $#argv < 2 ) then
echo "usage: $0 name age"
exit 1
endif
\end{lstlisting}
\end{frame}
\end{document}
Post Reply