[Tex/LaTex] Adjusting the start and ending position of |<->| arrows

arrowstikz-arrowstikz-pgf

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?

Best Answer

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}