3

After consulting this link: https://en.wikibooks.org/wiki/LaTeX/Source_Code_Listings in order to write some code, I concluded to this:

\lstset{ 
language=C, 
backgroundcolor=\color{highlight}, 
blah, blah, blah
}

\newcommand{\insertcode}[2]{\begin{itemize}\item[]\lstinputlisting[caption=#2,label=#1]{#1}\end{itemize}} 

Finally, I didn't use the newcommand, instead I used \lstinputlisting{my_file.c} and it worked fine.

(The file above is included int the main.tex file)

Now I want to add a second language, would that be something like

\lstset{ 
language=python, 
backgroundcolor=\color{highlight}, 
blah, blah, blah
}

Should that be included in a different file? Should I create a new command in the same file? Is so, what would that be? In either case, could someone provide an example of the newcommand structure?

Any thoughts would be appreciated.

2
  • Are your settings per language fundamentally different other than the language specification itself? If not, why not just pass the language to the \lstinputlisting command?
    – Alan Munn
    Jul 17, 2016 at 23:36
  • You are right. Without fiddling with the code, just \lstinputlisting[language=python]{my_file.py} did the trick. Thank you. Jul 17, 2016 at 23:45

1 Answer 1

3

There's no need to have separate \lstset commands for each language if all of your basic formatting for code snippets is the same.

Use \lstset to set the basic parameters of the code formatting, and then in your \lstinputlisting command you can just pas the language as a parameter:

\lstinputlisting[language=C]{file.c}
\lstinputlisting[language=python]{file.py}

If you do want to have different styles for different languages, you can do so using the \lstdefinestyle command. Here's an example of different styles used for LaTeX and Ruby:

\documentclass{article}
\begin{filecontents}{hello.rb}
class Hello
   def initialize(name)
      @name = name.capitalize
   end
   def sayHi
      puts "Hello #{@name}!"
   end
end

hello = Hello.new("world")
hello.sayHi
\end{filecontents}
\begin{filecontents*}{hello.tex}
\documentclass{article}
\begin{document}
Hello World!
\end{document}
\end{filecontents*}
\usepackage{listings}
\usepackage{xcolor}
\lstset{%
    basicstyle=\ttfamily\small,
    commentstyle=\itshape\ttfamily\small,
    showspaces=false,
    showstringspaces=false,
    breaklines=true,
    breakautoindent=true,
    captionpos=t
}
\lstdefinestyle{TeX}{language=[LaTeX]TeX, frame=none,texcsstyle=*\color{blue}}
\lstdefinestyle{ruby}{language=Ruby, frame=leftline,keywordstyle=\color{red},numbers=left}
\begin{document}
\section{Sample LaTeX document}
\lstinputlisting[style=TeX]{hello.tex}

\section{Sample Ruby document}
\lstinputlisting[style=ruby]{hello.rb}
\end{document}

output of code

2
  • is it actually possible to define multiple lstset without having them interfere with each other?
    – naphaneal
    Jul 18, 2016 at 16:13
  • @naphaneal No not exactly, but you can define your own styles. I've added an example to show how to do that.
    – Alan Munn
    Jul 18, 2016 at 16:47

You must log in to answer this question.

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