1

I want to create a documentation .pdf that shows examples of code for my commands and also what the result is.

To prevent differences between the description and the result I thought about creating a command to handle that:

\documentclass{article}

\NewDocumentCommand{\example}{m} { #1 \par \begin{verbatim} #1 \end{verbatim} }

\NewDocumentCommand{\myfunction}{O{default}m} { #1: This should be #2 }

\begin{document} \example{\myfunction[Nobody]{quite easy}} \end{document}

The problems seems to be the verbatim section since it does not only get the contents of #1 but seems to take whatever is put into the brackets of \example until \end{verbatim} is found (which never comes)... Is that the case? If so, how to prevent it?

P.S.: Using \verb|#1| also did not work...

mrCarnivore
  • 1,505
  • 2
    you can not use verbatim in the argument of a command (you can not make an egg out of an omelette) – David Carlisle May 15 '23 at 08:18
  • @DG': Not really. I want it the other way around. I want to expand one variable inside a \verb environment not the other way around. – mrCarnivore May 15 '23 at 08:18
  • @DavidCarlisle: I do not want to use verbatim inside the argument. I want to do the other way around. I want to use the argument as verbatim. – mrCarnivore May 15 '23 at 08:20
  • 2
    no you want to put verbatim inside \NewDocumentCommand, so it fails before you consider what to put in the verbatim – David Carlisle May 15 '23 at 08:20
  • 1
    If you're willing and able to compile your document under LuaLaTeX, the query How to handle verbatim material in LuaLaTeX should be of interest to you. Hint: All limitations with verbatim material inside the arguments of LateX commands can be overcome when using LuaLaTeX. – Mico May 15 '23 at 08:21
  • But how are the other documentations handling this? I surely can't be the first wanting to solve this problem... Is there another approach? – mrCarnivore May 15 '23 at 08:23
  • grab verbatim (v arg type) then use \scantokens (or write to a file and read back) to execute – David Carlisle May 15 '23 at 08:25
  • 5
    Don't reinvent the wheel. Doing this is not trivial. Look at the tcolorbox package, it has quite a lot very sophisticated environment s for side-by-side demonstration, see also https://ctan.org/topic/macro-demo – Ulrike Fischer May 15 '23 at 08:31

1 Answers1

0

With the help of Ulrike I was able to find a workaround in the package tcolorbox that does exactly what I needed:

\documentclass{article}

\usepackage{tcolorbox} \tcbuselibrary{listings}

\NewDocumentCommand{\myfunction}{O{default}m} { #1: This should be #2 }

\begin{document} \begin{tcblisting}{} \myfunction[Nobody]{quite easy} \end{tcblisting} \end{document}

enter image description here

mrCarnivore
  • 1,505