General\newcommand results in file ended while scanning for \next

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
CThule
Posts: 13
Joined: Tue Nov 10, 2015 7:46 pm

\newcommand results in file ended while scanning for \next

Post by CThule »

I am trying to turn it into a command, but I am having some trouble with this.
My listing that works, which I want to turn into a command.

Code: Select all

\begin{listing}
	\begin{tcblisting}{title = {Pseudocode},
			fonttitle=\sffamily,
			listing only, 
			listing remove caption=false, 
			label = lst:json2, 
			colframe = black,
			colback = white, 
			left=6mm, 
			bottom=-1mm, 
			top=0mm, 
			enhanced,
			boxrule=0.5pt, 
			attach boxed title to top left = {xshift=5mm, yshift=-3mm},
			boxed title style={colback=white, colframe=white}, 
			coltitle=black,
			sharp corners, 
			listing options={style=pseudo}}
		print "Hello, World!"
	\end{tcblisting}
	\caption{Example of a pseudocode block}
\end{listing}
My attempt to turn it into a command taking 3 arguments that invokes another command taking 5 arguments.

Code: Select all

\newcommand{\chnInnerListing}[5]
{
 \begin{listing}
 	\begin{tcblisting}{title = {{#1}},
 			fonttitle=\sffamily,
 			listing only, 
 			listing remove caption=false, 
 			label = {#3}, 
 			colframe = black,
 			colback = white, 
 			left=6mm, 
 			bottom=-1mm, 
 			top=0mm, 
 			enhanced,
 			boxrule=0.5pt, 
 			attach boxed title to top left = {xshift=5mm, yshift=-3mm},
 			boxed title style={colback=white, colframe=white}, 
 			coltitle=black,
 			sharp corners, 
 			listing options={style={#2}}}
{#5}
	\end{tcblisting}
 	\caption{{#4}}
 \end{listing}
}
	
\newcommand{\chnpseudo}[3]
{
\chnInnerListing{Pseudocode}{pseudo}{{#1}}{{#2}}{{#3}}
}

\chnpseudo{lst:pseudo4}{Example of a pseudocode block}{print "Hello, World!"}
Can any of you see what is wrong? I get the error:
File ended while scanning use of \next. \input{chapters/introduction/readingGuide}.

I have tried matching all the {}, and they seem to be right. I have also removing the extra wrapping around #number. I have tried removing spaces as well.

Recommended reading 2024:

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

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

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

\newcommand results in file ended while scanning for \next

Post by cgnieder »

You cannot put a verbatim environent (like {lstlisting}) inside of a command. Although LaTeX doesn't complain when you define the command it does produce an error when you try to use it:

Code: Select all

\documentclass{article}
\usepackage{listings}
\begin{document}

\newcommand\foo{\verb+x+}
\foo % => error

\newcommand\bla{%
  \begin{lstlisting}
    foo
  \end{lstlisting}%
}
\bla % => error

\end{document}
Regards
site moderator & package author
Post Reply