3

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};

1 Answers1

5

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:

enter image description here

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, commentstyle=\color{red}, 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:

enter image description here

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

Notes:

Code:

\documentclass{article}
\usepackage{tikz}

\newcommand\TikzCodeList{}% \newcommand\AddTikzCode[1]{\xdef\TikzCodeList{\TikzCodeList#1\endgraf}}% \AtEndDocument{\TikzCodeList}%

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

\newcommand\theCommand[1]{% \ShowAndExecuteNode[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}

Peter Grill
  • 223,288