Adjusting the start and ending position of |<->| arrows
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}
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:
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
which shows that the manual correction now overcorrects, of course....