General\newcommand with listings as parameter

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
SimonLatex
Posts: 3
Joined: Tue Aug 08, 2023 12:08 pm

\newcommand with listings as parameter

Post by SimonLatex »

I have defined a function called \test that just prints its parameter and before it prints the word "Code: ". In the document I call the function \test with a parameter that contains a listings enviroment. Unfortunately I always get an error message: Emergency stop. Fatal error occurred, no output PDF File produced.

How is it possible to write a own command that gets a listings environment as parameter?

Code: Select all

\documentclass[a4paper]{article}

\usepackage{listings}	% include listings to print source code in LaTeX

\lstnewenvironment{MyCode}{\lstset{language=C++}}{}	% Define a new listings environment

\newcommand{\test}[1]{Code: \\ #1}		% The following command prints Code: and the parameter given by the user



\begin{document}

% The following lines don't work
\test{
\begin{MyCode}
	while(BEDINGUNG)
	{
		CODE
	}
\end{MyCode}
}

\end{document}
Attachments
Skript.tex
(492 Bytes) Downloaded 232 times

Recommended reading 2024:

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

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

rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

\newcommand with listings as parameter

Post by rais »

You're facing a similar (if not the same) problem as explained in the FAQ about verbatim: https://texfaq.org/FAQ-verbwithin
you're basically hiding the end of the MyCode environment from itself this way.
Then again, why don't you put your `Code:' into the definition of MyCode? Then you won't need to surround it by \test{..}

Code: Select all

\documentclass[a4paper]{article}

\usepackage{listings}	% include listings to print source code in LaTeX

\lstnewenvironment{MyCode}{% Define a new listings environment
  Code:%
  \lstset{language=C++}}{}
 
\begin{document}

\begin{MyCode}
	while(BEDINGUNG)
	{
		CODE
	}
\end{MyCode}

\end{document}
Note that I discarded the \\ because some vertical space will be added before the actual listing starts, anyway.

KR
Rainer
SimonLatex
Posts: 3
Joined: Tue Aug 08, 2023 12:08 pm

\newcommand with listings as parameter

Post by SimonLatex »

Thank you for your answer! Yes, its the same problem as with the verbatim environment. The problem I described is a minimal example - the original file is a lot more complex. I use a self made command to style exercises for the exams of my students. The command gets the text of the exercise, the max. point to reach and additional things. No I want to put source code into the exercises. Therefore I have to give to the command the source code as an parameter because every exercise has an other source code... and thats the big problem. I tried more then 2 hours with \cprotect and \NewDocumentCommand\cmd{ m v m }{#1 `#2' #3} and other things, but nothing worked properly. Let me know, I you have another solution...
rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

\newcommand with listings as parameter

Post by rais »

Have you considered using an environment instead of a command? You could put a listing into another environment, even if it has arguments of its own:

Code: Select all

\documentclass[a4paper]{article}

\usepackage{listings}	% include listings to print source code in LaTeX

\lstnewenvironment{MyCode}{% Define a new listings environment
  \lstset{language=C++}}{}

\newenvironment{test}[2]{%
  #1%
  \edef\mytemp{#2}%
}{\mytemp}

\begin{document}

\begin{test}{before}{after}
  \begin{MyCode}
	while(BEDINGUNG)
	{
		CODE
	}
  \end{MyCode}
\end{test}

\end{document}
KR
Rainer
SimonLatex
Posts: 3
Joined: Tue Aug 08, 2023 12:08 pm

\newcommand with listings as parameter

Post by SimonLatex »

That's the solution! Thanks a lot!
Post Reply