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?

8
  • 3
    Never nest tikzpicture in tikzpicture. This make problems without externalizing.
    – Zarko
    May 6, 2021 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? May 6, 2021 at 21:49
  • You may bi satisfied with the following solution: \draw (1) to[edge node={node [sloped,above] {$\longrightarrow$}}] (2);
    – Zarko
    May 6, 2021 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.) May 6, 2021 at 22:00
  • 1
    See tex.stackexchange.com/questions/47377/… for a quite lengthy discussion about tikzpictures nesting
    – Rmano
    May 7, 2021 at 6:28

1 Answer 1

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

1
  • 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? May 7, 2021 at 12:32

You must log in to answer this question.

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