Verbatim in tikz node
One way is to save one or more verbbox
es 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}
Here is a version that demonstrates multiple verbbox
es, 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}
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}
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}