# Placing labels equidistant from a line segment (Version 2)

I have a display of similar triangles - one inscribed in another. I have five vertices that are labeled A, B, C, B_1, and B_2. Line segments BC and B_1C_1 are parallel with each other. I like the positions of the labels B and C with respect to the line segment BC. I would like to have the same (relative) positions of the B in the label B_1 and the C in the label C_1.

At the suggestion of Kpym, I used $C\smash{_{1}}$ to get C and C_1 typeset equidistant from the line segment. There is some artificial displacement of C_1 leftwards due to the subscript 1, though. (I also used the \smash command in typesetting $B\smash{_{1}}$.)

\documentclass{amsart}
\usepackage{amsmath}
\usepackage{amsfonts}

\usepackage{tikz}
\usetikzlibrary{calc,angles,positioning,intersections,quotes,decorations.markings}

\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\begin{document}

\begin{tikzpicture}

%Rays k and $\ell$ have a common endpoint at the origin, which is labeled A. With respect to
%the horizontal line through A, k is inclined at an angle of 50 degrees, and $\ell$ is
%inclined at an angle of 15 degrees. To place the label for A, the rays are extended 15pt by an
%invisible path command "before" A. The endpoints of these extensions are called "label_A_below"
%and "label_A_above" and the midpoint of the line segment between label_A_below and label_A_above
%is called "label_A".  A node command typesets "A" at the point 7.5pt from A on the invisible
%line segment between A and label_A .
\coordinate (A) at (0,0);
\draw[fill] (A) circle (1.5pt);

\coordinate (label_A_below) at (-130:15pt);
\coordinate (label_A_above) at (-165:15pt);
\coordinate (label_A) at ($(label_A_below)!0.5!(label_A_above)$);
\node[blue] at ($(A)!7.5pt!(label_A)$){$A$};

%B' is a point on ray k and C' is a point on $\ell$.
\coordinate (B') at (50:7);
\draw[name path=ray_k, -latex,] (A) -- (B');
\node (label_ray_k) at ($(B') + (50:7pt)$){$k$};
\coordinate (C') at (15:5.5);
\draw[name path=ray_ell, -latex,] (A) -- (C');
\node (label_ray_ell) at ($(C') + (15:6pt)$){$\ell$};

%This command labels a point "B" 3.75cm from A on ray $k$.
\coordinate (B) at (50:{15/4});
\draw (A) -- (B);

%These commands label the projection of AB onto \ell "C."  A line segment is drawn between
%B and C, and a right-angle mark is placed at C.
\coordinate (C) at ($(A)!(B)!(C')$);

\draw (B) -- (C) node [midway,right]{$y$};

\coordinate (U) at ($(C)!3mm!-45:(A)$);
\draw (U) -- ($(C)!(U)!(A)$);
\draw (U) -- ($(C)!(U)!(B)$);

%This command labels the length of AC.
\draw (A) -- (C) node [midway, below]{$x$};

%These commands draw an invisible line segment perpendicular to ray k from B to
%ray $\ell$. The intersection is labeled C_1. (The label for C_1 is placed after
%the point B_1 is defined.)
\path[name path=perpendicular_line_segment_from_B] (B) -- ($(B)!3.5cm!90:(A)$);
\coordinate[name intersections={of=perpendicular_line_segment_from_B and ray_ell,by={C_1}}];

%The following commands define B_1 as the intersection of k and a line segment
%perpendicular to $\ell$ through C_1. A line segment is drawn between
%B_1 and C_1, and a right-angle mark is placed at C_1. The labels "$B_{1}$" and "$C_{1}$"
%are typeset either directly above or directly below this line segment.
\path[name path=perpendicular_line_segment_from_C_1] (C_1) -- ($(C_1)!4cm!90:(C')$);
\coordinate[name intersections={of=perpendicular_line_segment_from_C_1 and ray_k, by={B_1}}];
\draw (C_1) -- (B_1) node [midway,right]{$y_{1}$};

\coordinate (U_1) at ($(C_1)!3mm!-45:(A)$);
\draw (U_1) -- ($(C_1)!(U_1)!(A)$);
\draw (U_1) -- ($(C_1)!(U_1)!(B_1)$);

%These commands put a brace below line segment AC_1 and label the length $x_{1}$.  This brace
%needs to be shifted downward from AC_1 to avoid interference with the labels A, C, and C_1.
%First, coordinates for A_shift and C_1_shift are defined to be 5mm from AC_1. A brace is drawn
%between A_shift  and D_shift.
\coordinate (A_shift) at ($(A)!5.5mm!-90:(C_1)$);
\coordinate (C_1_shift) at ($(C_1)!5.5mm!90:(A)$);
\draw[decorate,decoration={brace,raise=5pt,amplitude=5pt,mirror}] (A_shift) --
node[below right=4mm and 0.0625mm, fill=white, inner sep=1pt]{$x_{1}$} (C_1_shift);

%This command draws the two sides of the triangle with a common vertex A.
\draw (A) -- (B_1);
\draw (A) -- (C_1);

%These commands draw an invisible line segment 2.5mm above AB_1 for labels B and B_1.
\coordinate (A_shift_for_labels_version_1) at ($(A)!2.5mm!90:(B_1)$);
\coordinate (B_1_shift_for_labels) at ($(B_1)!2.5mm!-90:(A)$);
\coordinate (B_1_shift_for_labels_extension) at ($(B_1_shift_for_labels)!-0.25cm!(A_shift_for_labels_version_1)$);
\path[name path=path_for_labels_B_and_B_1] (A_shift_for_labels_version_1) -- (B_1_shift_for_labels_extension);
\coordinate (B_above) at ($(B)!-0.75cm!(C)$);
\path[name path=path_from_C_to_B_above] (C) -- (B_above);
\coordinate (B_1_above) at ($(B_1)!-0.75cm!(C_1)$);
\path[name path=path_from_C_1_to_B_1_above] (C_1) -- (B_1_above);
\coordinate[name intersections={of=path_for_labels_B_and_B_1 and path_from_C_to_B_above, by={B_label}}];
\coordinate[name intersections={of=path_for_labels_B_and_B_1 and path_from_C_1_to_B_1_above, by={B_1_label}}];
\node[green] at (B_label){$B$};
\node[blue] at (B_1_label){$B\smash{_{1}}$};

%These commands draw an invisible line segment 2.5mm below AC_1 for labels C and C_1. The center of the node
%containing $C_{1}$ is displaced upwards by the subscript "1" in  $C_{1}$. To avoid this upward vertical
%displacement, a \smash command is issued.
\coordinate (A_shift_for_labels_version_2) at ($(A)!2.5mm!-90:(C_1)$);
\coordinate (C_1_shift_for_labels) at ($(C_1)!2.5mm!90:(A)$);
\path[name path=path_for_labels_C_and_C_1] (A_shift_for_labels_version_2) -- (C_1_shift_for_labels);
\coordinate (C_below) at ($(C)!-0.75cm!(B)$);
\path[name path=path_from_B_to_C_below] (B) -- (C_below);
\coordinate (C_1_below) at ($(C_1)!-0.75cm!(B_1)$);
\path[name path=path_from_B_1_to_C_1_below] (B_1) -- (C_1_below);
\coordinate[name intersections={of=path_for_labels_C_and_C_1 and path_from_B_to_C_below, by={C_label}}];
\coordinate[name intersections={of=path_for_labels_C_and_C_1 and path_from_B_1_to_C_1_below, by={C_1_label}}];
\node[green] at (C_label){$C$};
\node[blue] at (C_1_label){$C\smash{_{1}}$};

\end{tikzpicture}

\end{document}


Do you want something like this? Note that you have to move the B_1 label up unless you want it to be printed over the line. So there is not much point in using \smash here if you are going to also apply the requested horizontal adjustment.

The horizontal adjustment is done by measuring the width of $_1$ and using half of that value as an offset for the labelling nodes.

\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{calc,angles,positioning,intersections,quotes,decorations.markings}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\begin{document}
\newlength\myshift
\settowidth{\myshift}{$_1$}
\begin{tikzpicture}

%Rays k and $\ell$ have a common endpoint at the origin, which is labeled A. With respect to
%the horizontal line through A, k is inclined at an angle of 50 degrees, and $\ell$ is
%inclined at an angle of 15 degrees. To place the label for A, the rays are extended 15pt by an
%invisible path command "before" A. The endpoints of these extensions are called "label_A_below"
%and "label_A_above" and the midpoint of the line segment between label_A_below and label_A_above
%is called "label_A".  A node command typesets "A" at the point 7.5pt from A on the invisible
%line segment between A and label_A .
\draw[fill] coordinate (A) circle (1.5pt);

\coordinate (label_A_below) at (-130:15pt);
\coordinate (label_A_above) at (-165:15pt);
\coordinate (label_A) at ($(label_A_below)!0.5!(label_A_above)$);
\node[blue] at ($(A)!7.5pt!(label_A)$){$A$};

%B' is a point on ray k and C' is a point on $\ell$.
\draw[name path=ray_k, -latex,] (A) -- (50:7) coordinate (B');
\node (label_ray_k) at ($(B') + (50:7pt)$){$k$};
\draw[name path=ray_ell, -latex,] (A) -- (15:5.5) coordinate (C');
\node (label_ray_ell) at ($(C') + (15:6pt)$){$\ell$};

%This command labels a point "B" 3.75cm from A on ray $k$.
\draw (A) -- (50:{15/4}) coordinate (B);

%These commands label the projection of AB onto \ell "C."  A line segment is drawn between
%B and C, and a right-angle mark is placed at C.
\draw (B) -- ($(A)!(B)!(C')$) coordinate (C) node [midway,right]{$y$};

\draw ($(C)!3mm!-45:(A)$) coordinate (U) -- ($(C)!(U)!(A)$);
\draw (U) -- ($(C)!(U)!(B)$);

%This command labels the length of AC.
\draw (A) -- (C) node [midway, below]{$x$};

%These commands draw an invisible line segment perpendicular to ray k from B to
%ray $\ell$. The intersection is labeled C_1. (The label for C_1 is placed after
%the point B_1 is defined.)
\path[name path=perpendicular_line_segment_from_B] (B) -- ($(B)!3.5cm!90:(A)$);
\coordinate[name intersections={of=perpendicular_line_segment_from_B and ray_ell,by={C_1}}];

%The following commands define B_1 as the intersection of k and a line segment
%perpendicular to $\ell$ through C_1. A line segment is drawn between
%B_1 and C_1, and a right-angle mark is placed at C_1. The labels "$B_{1}$" and "$C_{1}$"
%are typeset either directly above or directly below this line segment.
\path[name path=perpendicular_line_segment_from_C_1] (C_1) -- ($(C_1)!4cm!90:(C')$);
\coordinate[name intersections={of=perpendicular_line_segment_from_C_1 and ray_k, by={B_1}}];
\draw (C_1) -- (B_1) node [midway,right]{$y_{1}$};

\draw ($(C_1)!3mm!-45:(A)$) coordinate (U_1) -- ($(C_1)!(U_1)!(A)$);
\draw (U_1) -- ($(C_1)!(U_1)!(B_1)$);

%These commands put a brace below line segment AC_1 and label the length $x_{1}$.  This brace
%needs to be shifted downward from AC_1 to avoid interference with the labels A, C, and C_1.
%First, coordinates for A_shift and C_1_shift are defined to be 5mm from AC_1. A brace is drawn
%between A_shift  and D_shift.
\draw[decorate,decoration={brace,raise=5pt,amplitude=5pt,mirror}] ($(A)!5.5mm!-90:(C_1)$) coordinate (A_shift) --
node[below right=4mm and 0.0625mm, fill=white, inner sep=1pt] {$x_{1}$} ($(C_1)!5.5mm!90:(A)$) coordinate (C_1_shift);

%This command draws the two sides of the triangle with a common vertex A.
\draw (A) -- (B_1);
\draw (A) -- (C_1);

%These commands draw an invisible line segment 2.5mm above AB_1 for labels B and B_1.
\coordinate (A_shift_for_labels_version_1) at ($(A)!2.5mm!90:(B_1)$);
\coordinate (B_1_shift_for_labels) at ($(B_1)!2.5mm!-90:(A)$);
\coordinate (B_1_shift_for_labels_extension) at ($(B_1_shift_for_labels)!-0.25cm!(A_shift_for_labels_version_1)$);
\path[name path=path_for_labels_B_and_B_1] (A_shift_for_labels_version_1) -- (B_1_shift_for_labels_extension);
\path[name path=path_from_C_to_B_above] (C) -- ($(B)!-0.75cm!(C)$) coordinate (B_above);
\path[name path=path_from_C_1_to_B_1_above] (C_1) -- ($(B_1)!-0.75cm!(C_1)$) coordinate (B_1_above);
\coordinate[name intersections={of=path_for_labels_B_and_B_1 and path_from_C_to_B_above, by={B_label}}];
\coordinate[name intersections={of=path_for_labels_B_and_B_1 and path_from_C_1_to_B_1_above, by={B_1_label}}];
\node[green] at (B_label){$B$};
\node[blue, xshift=.5\myshift, yshift=.5em] at (B_1_label){$B_{1}$};

%These commands draw an invisible line segment 2.5mm below AC_1 for labels C and C_1. The center of the node
%containing $C_{1}$ is displaced upwards by the subscript "1" in  $C_{1}$. To avoid this upward vertical
%displacement, a \smash command is issued.
\coordinate (A_shift_for_labels_version_2) at ($(A)!2.5mm!-90:(C_1)$);
\coordinate (C_1_shift_for_labels) at ($(C_1)!2.5mm!90:(A)$);
\path[name path=path_for_labels_C_and_C_1] (A_shift_for_labels_version_2) -- (C_1_shift_for_labels);
\path[name path=path_from_B_to_C_below] (B) -- ($(C)!-0.75cm!(B)$) coordinate (C_below);
\path[name path=path_from_B_1_to_C_1_below] (B_1) -- ($(C_1)!-0.75cm!(B_1)$) coordinate (C_1_below);
\coordinate[name intersections={of=path_for_labels_C_and_C_1 and path_from_B_to_C_below, by={C_label}}];
\coordinate[name intersections={of=path_for_labels_C_and_C_1 and path_from_B_1_to_C_1_below, by={C_1_label}}];
\node[green] at (C_label){$C$};
\node[blue, xshift=.5\myshift] at (C_1_label){$C\smash{_{1}}$};

\end{tikzpicture}

\end{document}


If you don't mind the label writing over the line, you can, of course, remove the yshift and restore the \smash.

# EDIT

Here is proof that the C in C_1 is typeset as if the _1 had no area.

I add the following line just before the C_1 label is typeset:

   \node[red] at (C_1_label){$C$};


Since this node is red and the actual one is blue, the test is whether or not the red C is visible in the final result. As the following shows, it is not:

If the _1 was treated as having area, then the blue C in C_1 would be offset relative to the red C beneath it. But it is not.

• @user74973 See edit above for proof. If that is not what you mean, then I just don't get what you are asking. – cfr Jun 29 '15 at 1:19
• @user74973 The node is anchored at its centre by default. The subscript alters where the centre is, relative to the C. Specifically, it moves the C left by half of its width. Hence, moving the entire thing right by that amount cancels the displacement effect. If the node had an explicit anchor (e.g. anchor=west), then the effect would be different: the inclusion of the subscript would not affect the placement of the C. – cfr Jun 29 '15 at 15:04
• @user74973 Yes. Once you've declared the length you can set it as often as you want. If you need both values at the same time, you'd need to define an additional length with \newlength, of course. – cfr Jun 29 '15 at 15:40
• @user74973 Well, they are standard LaTeX commands so the standard resources for learning about LaTeX should cover them. Although the syntax is pretty simple: \newlength{\mylengthname} creates a new length, \mylengthname; \settowidth{\mylengthname}{some text} sets \mylengthname equal to the width of some text. Basically, LaTeX measures the width of a box with some text in it and then sets \mylengthname to have that value. It is analogous to the way that \newcounter etc. work. – cfr Jun 30 '15 at 16:36
• @user74973 Well... you are using them in LaTeX here! Any time you want something to match the width of something else, \settowidth is useful. Better than hard-coding a specific length since that would have to be redone if you change the font or font size of the document, whereas these commands ensure thing adapt fairly gracefully. And all kinds of packages need \newlength. Think of all the lengths in Beamer, for example - the length of side bars, depth of headlines and footlines, the height and width of text blocks etc. (I don't know if Beamer uses this or lower level TeX stuff.) – cfr Jun 30 '15 at 16:44