13

I am trying to have a verbatim environment in a tikz node just next to some other nodes.

I have tried

\documentclass{article}

\usepackage{tikz}

\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}
\node (s1) {$a$};
\node (s2) [right=of s1] {$b$};
\node (s3) [above right=0cm and 1cm of s2] {$c$};
\node (s4) [below right=0cm and 1cm of s2] {$d$};

\node[right=6cm of s2] {%

   verbatim environment

};

\path (s1) edge (s2);
\path (s2) edge (s3);
\path (s2) edge (s4);
\end{tikzpicture}

\end{document}

which produces

enter image description here

but if I add \begin{verbatim}...\end{verbatim}, it does not work any longer.

What I want is to have some nodes on the left and the some text in verbatim style on the right and a arrow from the nodes on the left to the text on the right.

4 Answers 4

15

One way is to save one or more verbboxes prior to entering the tikz. Then, recall them as needed.

\documentclass{article}

\usepackage{tikz,verbatimbox}

\usetikzlibrary{positioning}

\begin{document}
\begin{myverbbox}{\sTWO}
This i$ my \verbatim
  environment
\\##@
\end{myverbbox}

\begin{tikzpicture}
\node (s1) {$a$};
\node (s2) [right=of s1] {$b$};
\node (s3) [above right=0cm and 1cm of s2] {$c$};
\node (s4) [below right=0cm and 1cm of s2] {$d$};

\node[right=6cm of s2] {\sTWO};

\path (s1) edge (s2);
\path (s2) edge (s3);
\path (s2) edge (s4);
\end{tikzpicture}

\end{document}

enter image description here

Here is a version that demonstrates multiple verbboxes, the second one in color and \footnotesize:

\documentclass{article}

\usepackage{tikz,verbatimbox}

\usetikzlibrary{positioning}

\begin{document}
\begin{myverbbox}{\sTWO}
This i$ my \verbatim
  environment
\\##@
\end{myverbbox}
{\color{red}
\begin{myverbbox}[\footnotesize]{\sTWELVE}
Another
 verbatim &$#^_ environment
\end{myverbbox}
}

\begin{tikzpicture}
\node (s1) {$a$};
\node (s2) [right=of s1] {$b$};
\node (s3) [above right=0cm and 1cm of s2] {$c$};
\node (s4) [below right=0cm and 1cm of s2] {$d$};

\node[right=6cm of s2] {\sTWO};

\path (s1) edge (s2);
\path (s2) edge (s3);
\path (s2) edge (s4);

\node (s11) [below=2cm of s1] {$e$};
\node (s12) [right=of s11] {$f$};
\node (s13) [above right=0cm and 1cm of s12] {$g$};
\node (s14) [below right=0cm and 1cm of s12] {$h$};

\node[right=6cm of s12] {\sTWELVE};

\path (s11) edge (s12);
\path (s12) edge (s13);
\path (s12) edge (s14);
\end{tikzpicture}

\end{document}

enter image description here

4

You don't really need the verbatim text inside a node at all. If you take it out of the tikzpicture, you can just put it into a minipage or something to align it.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[baseline=(current bounding box.center)]
  \node (s1) {$a$};
  \node (s2) [right=of s1] {$b$};
  \node (s3) [above right=0cm and 1cm of s2] {$c$};
  \node (s4) [below right=0cm and 1cm of s2] {$d$};
  \draw (s1) -- (s2) edge (s3) -- (s4);
  \path [->] (s2 -| s4.east) edge ++(10mm,0);
\end{tikzpicture}
\begin{minipage}[c]{.5\linewidth}
\begin{verbatim}
  Some verbatim stuff
  in a
  minipage
\end{verbatim}
\end{minipage}
\end{document}

verbatim alongside diagram

2
  • 2
    I wish the down-voter would say why!
    – cfr
    Dec 27, 2016 at 16:30
  • (not the downvoter, just coming across this answer many years later, but it could be because in other cases it's necessary to place the verbatim text between the nodes so this answer is not as useful in that case...)
    – user202729
    Jan 27, 2023 at 14:16
3

While TikZ nodes support \verb in the node text they don't support the verbatim environment (for reasons I am not completely clear on).

But just for fun, how about rolling your own verbatim environment?

Here's a minimal-ish implementation which can automatically determine the line width (using the auto option). Alternatively the text width option to the node can be given which will wrap long lines. Otherwise you get one long line.

\documentclass[tikz,border=5]{standalone} 
\usetikzlibrary{positioning}
\makeatletter
\def\makeother#1{\catcode`#1=12\relax}
\def\makeactive#1{\catcode`#1=13\relax}
{
\catcode`\|=\catcode`\\
\catcode`\[=\catcode`\{
\catcode`\]=\catcode`\}
|makeother\\
|makeother|{
|makeother|}
|makeactive|^^M%
|long|gdef|docollect#1^^M\end{vrbtm}[|aftercollect[#1]]%
]
{
\makeactive\ %
\gdef\dospace{\makeactive\ \def {\spaceaction}}
}
{
\makeactive\^^M%
\gdef\doreturn{\makeactive\^^M\def^^M{\returnaction}}%
\gdef\beforecollect{\@ifnextchar^^M{\afterassignment\docollect\let\token=}%
  {\docollect}}%
}

\def\spaceaction{\hskip1ex}
\def\returnaction{\par\leavevmode}

\newbox\vrbbxa
\newbox\vrbbxb

\def\autobeforetypeset{%
  \let\returnaction=\autoline%
  \setbox\vrbbxa=\hbox\bgroup\ignorespaces}

\def\autoline{%
    \unskip%
  \egroup%
  \autoaddtobox%
  \setbox\vrbbxa=\hbox\bgroup\ignorespaces}

\def\autoaddtobox{%
  \ifdim\wd\vrbbxb<\wd\vrbbxa%
    \let\tmpbx=\vrbbxa%
  \else%
    \let\tmpbx=\vrbbxb%
  \fi%
  \setbox\vrbbxb=\vbox{\hsize=\wd\tmpbx%
    \ifvoid\vrbbxb\else\box\vrbbxb\fi\box\vrbbxa}%
}

\def\autoaftertypeset{%
   \unskip%
  \egroup%
  \autoaddtobox%
  \box\vrbbxb%
}

\def\beforetypeset{\noindent}
\long\def\typeset#1{#1}
\def\aftertypeset{\unskip}

\pgfkeys{vrbtm/.cd,
  auto/.code={%
    \let\beforetypeset=\autobeforetypeset
    \let\aftertypeset=\autoaftertypeset
  }
}
\def\vrbtm{\@ifnextchar[\vrbtmopts{\vrbtmopts[]}}
\def\vrbtmopts[#1]{%
  \begingroup%
    \pgfkeys{vrbtm/.cd,#1}%
    \let\do=\makeother%
    \dospecials%
    \dospace%
    \doreturn%
    \beforecollect}

\def\aftercollect#1{%
  \beforetypeset%
  \typeset{#1}%
  \aftertypeset%
  \endgroup%
  \end{vrbtm}%
}
\begin{document}

\begin{tikzpicture}[every node/.style={font=\ttfamily}]

\node[draw] (A) {%
\begin{vrbtm}[auto]
Here i$  $ome 
 verb&tim_text

with 
{automat~c} \line lengt# c%lcul%tion
\end{vrbtm}
};

\node[draw, below=of A, text width=4cm] (B) {%
\begin{vrbtm}
Here i$  $ome 
 verb&tim_text

with 
{te#t width} and word wrap
\end{vrbtm}
};

\node[draw, below=of B] (C) {%
\begin{vrbtm}
Here i$ $ome 
 verb&tim_text

 without
 {anything} \cle^er
\end{vrbtm}
};
\end{tikzpicture}
\end{document}

enter image description here

2

In 99% of the cases, if you can't use verbatim-like things inside something-else it's because of catcode problems.

In this case however, you'd guess that it's not the case because \verb works well.

Instead, the actual reason is that verbatim environment doesn't work in restricted horizontal mode (that is, the mode inside a \hbox).

As such, any solutions in Manual/automatic line breaks and text alignment in TikZ nodes - TeX - LaTeX Stack Exchange would work (as long as they don't, in addition, mess with the catcode)

For example, using text width attribute:

\documentclass{article}

\usepackage{tikz}

\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}
\node (s1) {$a$};
\node (s2) [right=of s1] {$b$};
\node (s3) [above right=0cm and 1cm of s2] {$c$};
\node (s4) [below right=0cm and 1cm of s2] {$d$};

\node[right=6cm of s2, text width=4cm] {%

\begin{verbatim}
verbatim environment
\end{verbatim}

};

\path (s1) edge (s2);
\path (s2) edge (s3);
\path (s2) edge (s4);
\end{tikzpicture}

\end{document}

There's an overfull hbox though. (6cm is too much)

Similarly, {minipage} environment works.

The solution using tabular doesn't work however (technical details, tabular uses the \halign primitive, which typesets the content inside each cell in restricted horizontal mode).

1
  • 1
    The same applies for displaymath environments from amsmath (e.g. align and variants). They just need a text width, a minipage or … Jan 27, 2023 at 14:04

You must log in to answer this question.

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