1

In the MWE below, there is a custom arrow defined as a separate drawing using \tikz. The arrow is used as an edge node so that it can be sloped automatically.

I am also using the TikZ external package because this is part of a book-length project with many TikZ figures.

MWE:

\documentclass{article}

\usepackage{tikz} \usetikzlibrary{external} \tikzexternalize

\newcommand{\myarrow}{\tikz{\draw[->] (0,0)--(10pt,0)}}

\begin{document}

\begin{tikzpicture}

    \node[draw] (1) at (0,0) {};
    \node[draw] (2) at (1,1) {};

    \draw (1) to[edge node={node [sloped,above] {\myarrow}}] (2);

    \end{tikzpicture}

\end{document}

If I try to compile the MWE as-is, I get an error message:

/mwe.tex:18: Package tikz Error: Sorry, the system call 'pdflatex -shell-escap
e -halt-on-error -interaction=batchmode -jobname "mwe-figure0" "\def\tikzextern
alrealjob{mwe}\input{mwe}"' did NOT result in a usable output file 'mwe-figure0
' (expected one of .pdf:.jpg:.jpeg:.png:). Please verify that you have enabled 
system calls. For pdflatex, this is 'pdflatex -shell-escape'. Sometimes it is a
lso named 'write 18' or something like that. Or maybe the command simply failed
? Error messages can be found in 'mwe-figure0.log'. If you continue now, I'll t
ry to typeset the picture.

See the tikz package documentation for explanation. Type H <return> for immediate help. ...

l.18 \end{tikzpicture}

? [1{/usr/local/texlive/2021/texmf-var/fonts/map/pdftex/updmap/pdftex.map}] (./mwe.aux) )</usr/local/texlive/2021/texmf-dist/fonts/type1/public/amsfonts/cm /cmr10.pfb> Output written on mwe.pdf (1 page, 8538 bytes). SyncTeX written on mwe.synctex.gz. Transcript written on mwe.log.

If I comment out the \tikzexternalize line, it compiles fine:

enter image description here

And if I comment out the \draw (1) ... line, it compiles fine too. Any suggestions?

  • 3
    Never nest tikzpicture in tikzpicture. This make problems without externalizing. – Zarko May 06 '21 at 21:48
  • Oh. I didn't realize that was a problem. Is there a particular reason it's a bad idea, or just that not all features are supported if you nest them? – LarrySnyder610 May 06 '21 at 21:49
  • You may bi satisfied with the following solution: \draw (1) to[edge node={node [sloped,above] {$\longrightarrow$}}] (2); – Zarko May 06 '21 at 21:54
  • 1
    Sometimes nesting works, mostly it doesn't. It has to do with conflicting global parameters. The general solution is to put one tikzpicture into a savebox and use \usebox, but for externalize it might be better to use a file and \includegraphics. (Or just use standalone instead.) – John Kormylo May 06 '21 at 22:00
  • OK, thanks, I didn't know that. I will use @Zarko's suggestion for the arrow. – LarrySnyder610 May 06 '21 at 22:16
  • You can use a sloped pic for the sloped arrow. This avoids nesting and gives you all the arrow heads and line styles you want. –  May 06 '21 at 22:24
  • @user241266 can you clarify? What is a sloped pic? – LarrySnyder610 May 06 '21 at 22:32
  • 1
    See https://tex.stackexchange.com/questions/47377/proper-nesting-of-tikzpicture-environments-reset-all-pgf-values-to-their-defaul for a quite lengthy discussion about tikzpictures nesting – Rmano May 07 '21 at 06:28

1 Answers1

2

Since this does not fit into a comment. It has already been pointed out that one should not nest tikzpictures. It is also not necessary, TikZ has a tool for what you are doing: pics. These can be sloped, too, and endowed with parameters (via pgf keys).

\documentclass{article}

\usepackage{tikz} \usetikzlibrary{external} \tikzexternalize

\begin{document}

\begin{tikzpicture}[pics/my arrow/.style={code={ \tikzset{my arrow/.cd,#1} \draw[my arrow/arrow] (-\pgfkeysvalueof{/tikz/my arrow/length}/2,\pgfkeysvalueof{/tikz/my arrow/y}) --(\pgfkeysvalueof{/tikz/my arrow/length}/2,\pgfkeysvalueof{/tikz/my arrow/y});}}, my arrow/.cd, arrow/.style={->},length/.initial=10pt,y/.initial={0pt}]

\node[draw] (1) at (0,0) {};
\node[draw] (2) at (1,1) {};

\draw (1) to pic[sloped]{my arrow={y=4pt}} (2);

\end{tikzpicture} \end{document}

enter image description here

  • This works but I'm having a little trouble parsing all the syntax. If I don't need the pic to size dynamically (i.e., I don't need it to take an argument), how would I simplify your code? – LarrySnyder610 May 07 '21 at 12:32