I have a problem with the lstlisting package. I want to define an \newenvironment with one obligatory parameter, which will be the caption name for the lstlisting. I will also have other lstlisting environments, that is why I am trying to define them separately.
I am also having some difficulties with \lstinline command (I set up a \newcommand to gain easier coding), which typesets keywords in normal text. Why can't it be in captions and similar?
I suppose something could be wrong with the preamble, that's why all the packages used are listed.
\documentclass{book}
\usepackage[a4paper, top=3cm, bottom=3cm, heightrounded, outer=3cm, inner=3cm, marginparwidth=1.5cm, marginparsep=0.5cm]{geometry}
\usepackage{tocloft}
\usepackage{amsmath}
\usepackage{eurosym}
\usepackage{url}
\usepackage{array}
\usepackage{tabularx}
\usepackage{multirow}
\usepackage{multicol}
\usepackage{graphics}
\usepackage[dvips]{graphicx}
\usepackage{epstopdf}
\usepackage[dvipsnames]{xcolor}
\usepackage{verbatim}
\usepackage{fancyvrb}
\usepackage{fancyhdr}
\usepackage{paralist}
\usepackage{listings}
\usepackage{pifont}
\usepackage{indentfirst}
\lstset{language=[Visual]Basic, %
keywordstyle=\color{blue},commentstyle=\color{ForestGreen},stringstyle=\color{Maroon},%
basicstyle=\ttfamily\normalsize, frame=lines, %
showspaces=false, showstringspaces=false,%
tabsize=4, aboveskip=10pt, belowskip=10pt, %
lineskip=2pt, numbers=left, numberstyle=\tiny, stepnumber=1, numbersep=5pt,%
numberblanklines=false, %
breaklines, breakatwhitespace, prebreak=\_, breakindent=0pt %
}
\lstset{morekeywords={Or, Loop, Until, To, As, Single, Module, Double, ByVal}}
\lstset{emph={Console}, emphstyle=\color{Aquamarine}}
\newenvironment{case}[1]{\renewcommand\lstlistingname{Case}%
\begin{lstlisting}[caption={#1}]}%
{\end{lstlisting}}
\newcommand{\code}[1]{\lstinline[basicstyle=\rmfamily\normalsize, prebreak=, keywordstyle=\ttfamily]{#1}}
\begin{document}
Example with inline, that works: \code{Console, Sub in Module}.
\begin{case}{Hello world} %doesn't work
Module Module1
Sub Main()
Console.WriteLine("Hello World!")
Console.ReadKey()
End Sub
End Module
\end{case}
\end{document}
Thank you in advance!
\lstnewenvironment? Have a look in the package manual or search this site! – cyberSingularity Dec 12 '12 at 13:25\codemacro, this is caused by trying to place verbatim-type material in a macro or as an argument. See eg Any ideas why this \newcommand{\n}{\lstinline|\n|} macro doesn't work? and Wrapping code (listings, verbatim, or other method) inside a newcommand – cyberSingularity Dec 12 '12 at 13:39begin{lstlisting}inside a user-defined environment?. @cmhughes: I am not sure if it has been fully resolved, but I have now posted an answer. (Note I do not have enough reputation to cast close votes anyway.) – cyberSingularity Dec 12 '12 at 17:49\section+\label, syntax highlight F#, complex macro, include in 2-column table, (this question) caption name, – user202729 Dec 28 '21 at 12:40