0

I wanted to create a parametrized macro (using newcommand) for my listings:

It works as long as I use text as input parameter. Once I switch to macros as parameter it doesn't work.

I have the following MWE:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\begin{document}
\def\A{name}
\def\C{value}
\newcommand{\paramListing}[4]{
\begin{lstlisting}^^J
#1(#2, #3, #4);^^J
\end{lstlisting}
}

\paramListing{func}{\A}{true}{\C} %prints 'func(A, true, C)', but should be 'func(name, true, value)'
\paramListing{func}{name}{true}{value} %prints func(name, true, value)
\end{document}

It produces two lines: one with func(A, true, C) (which is not what I wanted) and one with func(name, true, value). Therefore, I did some research and found that \expandafter should do the trick.

However, I don't know how to use it correctly (even with multiple parameters), because I still can't use it with a newcommand that accepts only one Parameter!

For example this MWE:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\begin{document}
\def\A{name}

\newcommand{\oneParamListing}[1]{
\begin{lstlisting}^^J
func(#1, true, value);^^J
\end{lstlisting}
}

\expandafter\oneParamListing\A %prints 'func(n, true, value)\name'
\expandafter\oneParamListing{\A} %prints 'func(A, true, value)'
\end{document}

which produces the line func(n, true, C) (which is the first char of the macro \A, so in my opinion it may be the right direction?) and the line func(A, true, C) which is also not what I wanted?.

I tried everything with pdfTeX 3.14159265-2.6-1.40.21.

Summarized I have two questions:

  • Is \expandafter what I need here? If yes, how can I use it?
  • Can I use it for macros with more than one parameters? Or do you have another approach here?
3
  • the example works, it prints everything in monospace font
    – byteunit
    Commented Dec 1, 2021 at 12:10
  • Indeed, my bad. I'll delete that comment. I think you want \expandafter\oneParamListing\expandafter{\A} Commented Dec 1, 2021 at 12:14
  • thanks, that fixes the second MWE!
    – byteunit
    Commented Dec 1, 2021 at 12:36

2 Answers 2

2

Meanwhile I found the answer on my own:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\begin{document}
\def\A{name}
\def\C{value}
\newcommand{\paramListing}[4]{
\begin{lstlisting}^^J
#1(#2, #3, #4);^^J
\end{lstlisting}
}

\edef\params{{func}{\A}{true}{\C}}
\expandafter\paramListing\params
\end{document}

This prints out func(name, true, value); as expected.

0

It is easy to do real function composition with functional package. In the following code, \Value is a predefined function.

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage{listings}
\usepackage{functional}

\begin{document}

\def\A{name}
\def\C{value}

\PrgNewFunction \paramListing {mmmm} {
\begin{lstlisting}^^J
#1(#2, #3, #4);^^J
\end{lstlisting}
}

\paramListing{func}{name}{true}{value}
\paramListing{func}{\Value\A}{true}{\Value\C}

\end{document}

enter image description here

You must log in to answer this question.

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