6

I want to draw lines for dimensions with the command \draw[|<->|]...;. In addition, I have line to some other objects, whose dimensions should be stated. However, the lines from the arrow option |<->| are a bit shifted compared to the perpendicular line at the same coordinates. Is there a way to correct this automatically and not manually, like I did it in the example? Subtracting the half line width is only applicable, if the direction of the arrow is known, and only easy, if it's along the coordinate system.

MWE

\documentclass[convert={density=1000}]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[gray] (0,0) -- (0,0.5);
\draw[gray] (1,0) -- (1,0.5);
\draw[|<->|] (0,0.125) -- (1,0.125);
\draw[|<->|] (0-\pgflinewidth/2,0.375) -- (1cm+\pgflinewidth/2,0.375);
\end{tikzpicture}
\end{document}

Output

enter image description here

Edit:

The suggested answer uses \pgfarrowsdeclare, which seems to be deprecated, as stated in this question. I'll try to solve my question with the current command, \pgfdeclarearrow. With it I can simply write

\pgfdeclarearrow{name=dim-dim,   means={>.|[sep=-0.25pt]},}

which is a very short solution and almost does what I want. The only drawback is that it is not possible for me to use \pgflinewidth instead of the manual value -0.25pt. This results in the error Missing \endcsname inserted., if I try to compile the following code:

\documentclass[]{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
%\pgfdeclarearrow{name=dim-dim,   means={>.|[sep=-0.25pt]},}
\pgfdeclarearrow{name=dim-dim,   means={>.|[sep=-\pgflinewidth]},}
\begin{document}
\begin{tikzpicture}
\draw[gray] (0,0) -- (0,1);
\draw[gray] (1,0) -- (1,1);
\draw[dim-dim] (0,0.5) -- (1,0.5);
\end{tikzpicture}
\end{document}

Is there a way to overcome this error?

2

2 Answers 2

5

This is a more official way to do so (Sorry about the language)(More explanation below)

\documentclass[tikz,margin=9]{standalone}
\usetikzlibrary{arrows.meta}
\begin{document}
    \begin{tikzpicture}
        \draw[gray] (0,0) -- (0,0.5);
        \draw[gray] (1,0) -- (1,0.5);
        \draw[{|[sep=0 -.5].<}-{[line width=0 3]>.|[sep=0 -1.5]}](0,0.375) -- (1,0.375);
        \draw[{|[sep=0 .5].<}-{>[sep=0 1].|[sep=0 -1.5]}](0,0.125) -- (1,0.125);
    \end{tikzpicture}
\end{document}

Explanation

Arrow tips are carefully arranged not to exceed the end of the segment (or curves) so that it will never overlap anything important. This happens on every arrow tips (see image below from the manual)

And of course it happens on | by default, which leads to your question.

We can fix this by adding spaces, positive or negative. Only two questions: where? and how much?

It turns out that /pgf/arrow keys/ has a sep option to control the separation of arrow tips. This is much like we add \vspace{1ptplus1fil} or whatever in our document. So we may add a negative space so make the tip exceeding the end, just like \vspace{-1pt}.

But how much? The sep option actually accepts three parameters. The second parameter is the line width factor. That is to say, sep=0 2 is a positive space equals to twice the line width, while sep=0 -.5 is a negative space equals to half of the line width. The latter is what we want, as you can see at the upper left corner.

This is not the end. Since we can change the line width of arrow tips independently, we can play with the second parameter of /arrow keys/line width and /arrow keys/sep. You can see some variation at other three corners.

Things get crazy once double involved. But still it is possible to play with the third parameter.

\begin{tikzpicture}[scale=2]
    \draw[double,gray] (0,0) -- (0,0.5);
    \draw[double,gray] (1,0) -- (1,0.5);
    \draw[double,{|[sep=0 -.5]<.}-{[line width=0 1].>[sep=0]|[sep=0 -.5]}](0,0.375) -- (1,0.375);
    \draw[double,{|[sep=0 -.5 2]<.}-{[line width=0 -1 2].>[sep=0 1 1]|[sep=0 .5 2]}](0,0.125) -- (1,0.125);
\end{tikzpicture}
6
  • 1
    Is shorten > not official? It is in the manual!
    – cfr
    Jan 8, 2016 at 2:23
  • 1
    @cfr It is official. But it fails if the line width of arrows varies. I am saying it is more <del>official</del> general.
    – Symbol 1
    Jan 8, 2016 at 2:24
  • If you say that, I'd like to vote for your answer, but it seems misleading to say it is more official. (May well be better - I don't doubt that.)
    – cfr
    Jan 8, 2016 at 2:29
  • @cfr I sometimes run out of word. I am editing my answer.
    – Symbol 1
    Jan 8, 2016 at 2:30
  • My style version works so long as you change the line width before calling the style. That is line width=1mm, double trouble seems to do the right think. But that's not entirely ideal since you have to remember the order.
    – cfr
    Jan 8, 2016 at 2:34
4

Is a style an option?

For example:

\documentclass[border=5pt, multi, tikz]{standalone}
\begin{document}
\begin{tikzpicture}
  [
  double trouble/.style={|<->|, shorten >=-.5\pgflinewidth, shorten <=-.5\pgflinewidth}
  ]
  \draw[gray] (0,-0.5) -- (0,0.5);
  \draw[gray] (1,-0.5) -- (1,0.5);
  \draw[|<->|] (0,0.125) -- (1,0.125);
  \draw[|<->|] (0-\pgflinewidth/2,0.375) -- (1cm+\pgflinewidth/2,0.375);
  \draw[double trouble] (0,-0.125) -- (1,-0.125);
\end{tikzpicture}
\end{document}

shows that double trouble is properly aligned:

double trouble

Of course, you can set the shorten > and shorten < for the whole picture if you want this to apply to all arrow tips.

\begin{tikzpicture}
  [
    shorten >=-.5\pgflinewidth,
    shorten <=-.5\pgflinewidth,
  ]
  \draw[gray] (0,-0.5) -- (0,0.5);
  \draw[gray] (1,-0.5) -- (1,0.5);
  \draw[|<->|] (0,0.125) -- (1,0.125);
  \draw[|<->|] (0-\pgflinewidth/2,0.375) -- (1cm+\pgflinewidth/2,0.375);
  \draw[|<->|] (0,-0.125) -- (1,-0.125);
\end{tikzpicture}

produces

picture-wide trouble brewing

which shows that the manual correction now overcorrects, of course....

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.