4

I would like to adjust the location of a node on a path automatically when there is a coordinate transformation on the path. In the example below, I cheated and manually adjusted the location of the nodes.

MWE:

\documentclass[]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\def \rotationangle {120};

%Line without a rotation
\begin{scope}[rotate=0];
   \draw(0,0) -- (5cm,0)
   node[at start, left]{a}
   node[at end, right]{b}
   node[midway, above]{\tiny a) Unrotated};
\end{scope}

%Line with a rotation, but nodes are not aligned with line axis
\begin{scope}[yshift=1cm, rotate=\rotationangle];
   \draw(0,0) -- (5cm,0)
   node[at start, left]{a}
   node[at end, right]{b}
   node[midway]{\tiny b)Rotated, but nodes are not aligned};
\end{scope}

%Line with rotation, nodes are aligned with line axis with a cheat.
%Can the nodes be aligned with w.r.t. the rotation automatically. 
\begin{scope}[yshift=1cm, , xshift=5cm,  rotate=\rotationangle];
   \draw(0,0) -- (5cm,0)
   node[at start, xshift=0.15cm, yshift=-0.2cm]{a}   %Cheated with xshift and yshift
   node[at end, xshift=-0.35cm, yshift=0.25cm, anchor=west]{b} %Cheated with xshift and yshift
   node[midway]{\tiny c)Rotated, nodes are aligned, but cheated};   
\end{scope}

\end{tikzpicture}
\end{document}

Output:

enter image description here

I can write a code to make the adjustment with for example, tikzmath, and make it automated. However, I wonder if there is a built-in-Tikz way of doing this.

  • You could perhaps do \draw node (a) at (0,0) {a} node (b) at (5cm,0) {b} (a) -- (b) node[midway]{\tiny b)Rotated, nodes are aligned};, but the result is not the same as in your code. – Torbjørn T. Jul 18 '17 at 9:44
  • @TorbjørnT Is there a way to apply your solution while keeping the line length 5cm? – berkus Jul 18 '17 at 10:26
  • No, but I thought of a simpler solution, see my answer below. – Torbjørn T. Jul 18 '17 at 11:24
4

You can just set an appropriate anchor for the two nodes, based on the \rotationangle.

\documentclass[]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\def \rotationangle {120};

\begin{scope}[rotate=\rotationangle];
   \draw (0,0) -- (5cm,0)
   node[circle,anchor=\rotationangle,pos=0] {a}
   node[circle,anchor=\rotationangle+180,pos=1] {b}
   node[midway]{\tiny b)Rotated, nodes are aligned};
\end{scope}

\end{tikzpicture}
\end{document}
3

Load the calc library and position nodes a and b along the direction (0,0) -- (5cm,0), or the reverse direction, with a fixed offset of your choice (1em below). The syntax is like this: ($(point1) ! distance from point1 along the direction point1 -- point2 ! (point2)$).

\documentclass[]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}
\def \rotationangle {120};

%Line without a rotation
\begin{scope}[rotate=0];
   \draw(0,0) -- (5cm,0)
   node[at start, left]{a}
   node[at end, right]{b}
   node[midway, above]{\tiny a) Unrotated};
\end{scope}

%Line with a rotation, but nodes are not aligned with line axis
\begin{scope}[yshift=1cm, rotate=\rotationangle];
   \draw(0,0)coordinate(1) -- (5cm,0)coordinate(2)
   node at ($(1)!-1em!(2)$) {a}
   node at ($(2)!-1em!(1)$) {b}
   node[midway]{\tiny b)Rotated, and nodes are aligned};
\end{scope}

%Line with rotation, nodes are aligned with line axis with a cheat.
%Can the nodes be aligned with w.r.t. the rotation automatically. 
\begin{scope}[yshift=1cm, , xshift=5cm,  rotate=\rotationangle];
   \draw(0,0) -- (5cm,0)
   node[at start, xshift=0.15cm, yshift=-0.2cm]{a}   %Cheated with xshift and yshift
   node[at end, xshift=-0.35cm, yshift=0.25cm, anchor=west]{b} %Cheated with xshift and yshift
   node[midway]{\tiny c)Rotated, nodes are aligned, but cheated};   
\end{scope}

\end{tikzpicture}
\end{document}

enter image description here

1

An example where the solution provided by Torbjørn T. can be used:

\documentclass[]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\def \rotationangle {168};
\begin{scope}[rotate=\rotationangle];
   \draw (0,0)  node[circle,anchor=\rotationangle+45]{a}
   -- ++(5cm,0) node[circle,anchor=\rotationangle+135]{b}
   -- ++(0,5cm) node[circle,anchor=\rotationangle+225]{c}
   -- ++(-5,0)  node[circle,anchor=\rotationangle+315]{d}
   -- cycle;
   \node at (2.5,2.5) {Angle = \rotationangle degrees};
   \draw[fill=black] (0,0) circle[radius=2pt];
\end{scope}
\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.