# Print macro output instead of executing

I am attempting to debug a Tikz/PGF script. I have a STY file that generates Tikz/PGF scripts, but the output graphic isn't quite what I want it to be.

In order to debug the problem, I was hoping that I could tweak my code so that the Tikz/PGF script is printed to the LaTeX document (in a verbatim environment, for example) instead of being executed by tikzpicture.

Here is a small example of what I am after:

\documentclass[a4paper]{report}

\usepackage{tikz}

\newcommand\theCommand[1]{%
\node [rectangle, draw, fill=blue!20, text width=5em, text centered, rounded corners, minimum height=4em] {#1};%
}

\begin{document}
\begin{tikzpicture}
\theCommand{test}
\end{tikzpicture}
\end{document}

This code would draw a rectangular box with the text "test" inside. However, ideally I instead want this to print out the text:

\node [rectangle, draw, fill=blue!20, text width=5em, text centered, rounded corners, minimum height=4em] {test};
-
Using verbatim is an option? –  azetina Sep 23 '12 at 4:00
@azetina Yes, but just changing tikzpicture to verbatim doesn't have the desired effect as it will print "\theCommand{test}" instead –  Joshua Spence Sep 23 '12 at 4:27
Outside the tikzpicture environment, issuing {\ttfamily\meaning\theCommand} gives the meaning of \theCommand (without passing arguments to it). See, for example, Equivalent of \show to display the LaTeX code in the document and The definitions of LaTeX commands. –  Werner Sep 23 '12 at 5:41
@JoshuaSpence Do you want the output of \tracingall? If so, you are looking at a lot of code for any realistic TikZ picture! –  Joseph Wright Sep 23 '12 at 9:15

Not sure how useful this is for you, but here are two options. Perhaps you can adapt one for your particular case:

## 1. showexpl Package:

I would recommend you use the showexpl package, which will typeset the content and also list the LaTeX code:

## Code:

\documentclass{article}
\usepackage{showexpl}
\usepackage{tikz}

\lstdefinestyle{myLatexStyle}{
language=TeX,
basicstyle=\small\ttfamily,
backgroundcolor=\color{yellow},
numbers=left, numberstyle=\tiny, stepnumber=2, numbersep=5pt,
showstringspaces=false,
keywordstyle=\color{blue}\bfseries,
morekeywords={align,begin},
pos=l
}

\begin{document}
\begin{LTXexample}[style=myLatexStyle, pos=b]
\begin{tikzpicture}
\node [rectangle, draw, fill=blue!20, text width=5em, text centered,
rounded corners, minimum height=4em] {test};
\end{tikzpicture}
\end{LTXexample}
\end{document}

## 2. Replace \node macro:

The above solution will only show the code that is within the LTXexample environment. So if you still wanted to use a separate drawing macro, the above would only show \theCommand{test}; in the output, which does not provide much information. An alternative is if you replace:

\newcommand\theCommand[1]{%
\node [rectangle, draw, fill=blue!20, text width=5em, text centered,
rounded corners, minimum height=4em] {#1};%
}

to use \ShowAndExecuteNode instead of \node:

\newcommand\theCommand[1]{%
\ShowAndExecuteNode[rectangle, draw, fill=blue!20, text width=5em, text centered,
rounded corners, minimum height=4em]{#1}%
}

then you get the output:

Commenting out the \AtEndDocument{\TikzCodeList} will eliminate this output.

## Code:

\documentclass{article}
\usepackage{tikz}

\newcommand\TikzCodeList{}%
\AtEndDocument{\TikzCodeList}%

\newcommand{\ShowAndExecuteNode}[2][]{%
\node [#1] {#2};