[Tex/LaTex] Execute own command in listing

listingsmacros

I create an own command like this

\newcommand{\hostname}{this.is.my.hostname}

Now I want to use this command in normal text (no problem) and inside of a listing

Nun kann die Funktionsweise überprüft werden:

\begin{lstlisting}[language=BashGriduser]
su -l griduser
globus-url-copy gsiftp://\hostname:2811/home/share/test file:///home/share/test2
\end{lstlisting}

In the text, the command is executed without any problems. Inside the listing it is printed as \hostname. How can I execute this inside the listing or pass this as parameter or something like that.

Best Answer

You can "escape" things as follows:

\documentclass{article}
\usepackage{listings}
\lstset{%
  escapeinside={(*}{*)},%
}
\newcommand\foo{Bar}
\begin{document}
\begin{lstlisting}
  Text (*\foo*)
\end{lstlisting}
\end{document}

Enclose macros you want tex to actually read inside some "escaping" mechanism defined by escapeinside

Related Question