I don't really know anything about listings
or multicol
and particularly haven't a clue as to why they complain so much about being put in a box (presumably to do with how multicol
works its magic). So I went back to the original specification: to get a listings
listing in a columnar setup within a TikZ node. I came up with the following solution. What seemed to be the key was to avoid precisely that situation that your code lands in: that something reads in the listing ahead of time and then resets it somewhere else. Several of the commands that I chose to use had a "read ahead" version, and I found that the code did not work if I chose that one. For example, I had to use a minipage
instead of a \parbox
.
Anyway, this will need tweaking I'm sure - particularly with regard to line widths - but it does seem to do the basic "put a 2-column listing in a TikZ node".
\documentclass{article}
\usepackage{listings}
\usepackage{tikz}
\usepackage{multicol}
\tikzstyle{sourcecodebox} = [
fill=white, draw=black, inner sep=1em, rectangle
]
\makeatletter
\lstnewenvironment{lstfigurelisting}[1][]{%
\tikzpicture
\node[sourcecodebox] (box)
\bgroup
\minipage{.75\textwidth}
\lstset{frame=none,#1}
}
{
\endminipage
\egroup;
\endtikzpicture
}
\makeatother
\begin{document}
Some code here:
\begin{lstfigurelisting}[multicols=2]
#!/usr/bin/env python
def main():
print "Hello, World!"
if __name__ == '__main__':
main()
\end{lstfigurelisting}
\end{document}
Result:
Updated Version:
Here is an updated version which addresses your requirements:
- Double slash are comments are in gray and italics
- Strings in single quote are green
- Literals (written in all caps) are dark blue (but need to be specified)
options{}
is in black until a trailing }
is encountered.
- Everything else is magenta
- Line breaking is enabled (see lines 5-7)
Code:
\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\lstset{% This applies to ALL lstlisting
backgroundcolor=\color{yellow!10},%
numbers=left, numberstyle=\tiny, stepnumber=2, numbersep=5pt,%
}%
% Applies only when you use it
\lstdefinestyle{MyLang}{
basicstyle=\small\ttfamily\color{magenta},%
breaklines=true,% allow line breaks
moredelim=[s][\color{green!50!black}\ttfamily]{'}{'},% single quotes in green
moredelim=*[s][\color{black}\ttfamily]{options}{\}},% options in black (until trailing })
commentstyle={\color{gray}\itshape},% gray italics for comments
morecomment=[l]{//},% define // comment
emph={%
STRING% literal strings listed here
},emphstyle={\color{blue}\ttfamily},% and formatted in blue
alsoletter={:,|,;},%
morekeywords={:,|,;},% define the special characters
keywordstyle={\color{black}},% and format them in black
}
\begin{document}
\begin{lstlisting}[style=MyLang]
options {
language=Java;
//backtrack=true;
// please comment it in for correct lexer generation!
// the definitions of many operators require backtracking!
}
// program blocks
program
: (declaration)+
;
declaration
: metaDeclaration
| actionDeclaration
| globalDeclaration
| eventDeclaration
;
metaDeclaration
: 'meta' metaParam STRING? ';'
;
metaParam
: 'version'
| 'name'
| 'icon'
| 'color'
| 'private'
;
globalDeclaration
: 'var' varIdentifier ':' typeIdentifier
codeblock
;\end{lstlisting}
\end{document}
Initial Version
Adapting the solution from Extend a language with additional keywords? should get you started:
\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\lstset{%
backgroundcolor=\color{yellow!20},%
basicstyle=\small\ttfamily\color{blue},%
numbers=left, numberstyle=\tiny, stepnumber=2, numbersep=5pt,%
}%
% Add your keywords here, and have this in a separate file
% and include it in your preamble
\lstset{emph={%
color, icon, meta, name, private, var%
},emphstyle={\color{green}}%
}%
\begin{document}
\begin{lstlisting}
metaParam
: 'version'
| 'name'
| 'icon'
| 'color'
| 'private'
;
globalDeclaration
: 'var' varIdentifier ':' typeIdentifier
codeblock
;
\end{lstlisting}
\end{document}
Best Answer
,breakatwhitespace,gobble=10
is an alternative solution: