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

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

3
  • Do you know where to look in the manual for "\the"? (my viewer(mac preview) refuses to search for "\the") Jun 21 at 10:50
  • 1
    @hpekristiansen See tex.stackexchange.com/q/38674/4427
    – egreg
    Jun 21 at 12:04
  • Great. very handy solution. Now I understand that TikZ does not takes LaTeX lengths (registers) in the option length, but just "text". Jun 21 at 12:38
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.