5

I draw an arrow in Tikz and I want to specify the length with a LaTeX length, e.g., \headlength, instead of explicitly writing the length, e.g., 2em. However, it seems that the TikZ does not like LaTeX lengths as input for the length of the arrow head. (See example below)

How could I give a LaTeX length to the option length=? (Thanks!)

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}

\begin{document}

\begin{tikzpicture} \draw[-{Latex[length=2em]}] (0,0) -- (2,0); %It works! \newlength{\headlength} \setlength{\headlength}{2em} \draw[-{Latex[length=\headlength]}] (0,1) -- (2,1); %It outputs error \end{tikzpicture}

\end{document}

enter image description here

2 Answers2

6

You can use \the\headlength.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}

\newlength{\headlength}

\begin{document}

\begin{tikzpicture} \setlength{\headlength}{2em} \draw[-{Latex[length=2em]}] (0,0) -- (2,0); %It works! \draw[-{Latex[length=\the\headlength]}] (0,1) -- (2,1); %Works as well \end{tikzpicture}

\end{document}

Allocate registers in the preamble, not inside the tikzpicture.

enter image description here

egreg
  • 1,121,712
1

I found a quick fix via math library (I am not satisfied: it looks like a patch and I do not understand why it works)

Just store the LaTeX length in a tikzmath variable and voilà.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{math}

\begin{document}

\newlength{\headlength}

\begin{tikzpicture} \draw[-{Latex[length=2em]}] (0,0) -- (2,0); \setlength{\headlength}{2em} \tikzmath{ % \mylength=\headlength; %Storing the length in a tikzmath variable } \draw[-{Latex[length=\mylength]},red] (0,1) -- (2,1); \end{tikzpicture}

\end{document}

enter image description here