3

Is there some way to specify an 'absolute print distance' in TikZ so that even if an image is scaled, some distances are kept intact and are printed at the specified distance? E.g.

\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (4,0);
\draw (A)--node[below]{4}(B);
\node at ($(A)+(180:.3cm)$){A};
\node at ($(B)+(0:.3cm)$){B};
\end{tikzpicture}
\hskip5mm
\begin{tikzpicture}[scale=0.1]
\coordinate (A) at (0,0);
\coordinate (B) at (40,0);
\draw (A)--node[below]{40}(B);
\node at ($(A)+(180:.3cm)$){A};
\node at ($(B)+(0:.3cm)$){B};
\end{tikzpicture}

enter image description here

Note how "below" remains the same, but the distance for the node text is smaller on the scaled version. Is there some way to specify the ”.3cm” so that it is .3cm at the printout, independently of the scaling of the image? TIA.

mf67
  • 666
  • 1
    When increasing/decreasing sizes of tikzpictures I find that \resizebox gives better results than scale. But if the 0.3cm is that important why not create the picture in a desired size rather than way too big so that you have to scale/resize? – Plergux Nov 14 '20 at 14:03
  • When copying drawings I don't want to "rethink" the size or scale it manually, just use the dimensions used in the image and scale it to a good size with scale=... But at the same time I don't want to recalculate all the distances so I get an equidistant space for all node texts, just like node[below] does in the shown example. That distance does not change, even if scaled. – mf67 Nov 14 '20 at 14:18

2 Answers2

4

Labels set at a specified distance to coordinates or lines are not affected by scaling, so label the ends of the lines with A and B instead of constructing the labels as separate nodes.

enter image description here

\documentclass[border=2pt,tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate[label={left=0.3cm:A}] (A) at (0,0);
\coordinate[label={right=0.3cm:B}] (B) at (4,0);
\draw (A)--node[below]{4}(B);
\end{tikzpicture}
\begin{tikzpicture}[scale=0.1]
\coordinate[label={left=0.3cm:A}] (A) at (0,0);
\coordinate[label={right=0.3cm:B}] (B) at (40,0);
\draw (A)--node[below]{40}(B);
\end{tikzpicture}
\end{document}

TikZ offers various options to place the labels. See section "17.10.2 The Label Option" of the TikZ manual for details. Below, the labels are shifted up by 3mm (in addition to their horizontal distance).

enter image description here

\documentclass[border=2pt,tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate[label={[yshift=3mm]left=3mm:A}] (A) at (0,0);
\coordinate[label={[yshift=3mm]right=3mm:B}] (B) at (4,0);
\draw (A)--node[below]{4}(B);
\end{tikzpicture}
\begin{tikzpicture}[scale=0.1]
\coordinate[label={[yshift=3mm]left=3mm:A}] (A) at (0,0);
\coordinate[label={[yshift=3mm]right=3mm:B}] (B) at (40,0);
\draw (A)--node[below]{40}(B);
\end{tikzpicture}
\end{document}
gernot
  • 49,614
0

Another possibility: coordinate [xshift=..., yshift=...] are not affected by scaling.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\pagenumbering{gobble}
\begin{tikzpicture}[scale=2]
  %\draw (0, 0) -- (1, 1) -- ++(1, 0);              % doesn't work
  %\draw (0, 0) -- (1, 1) -- ++([xshift=1cm] 0, 0); % doesn't work
  \draw (0, 0)
    -- (1, 1)
    coordinate [xshift=1cm] (tmp)
    -- (tmp);
\end{tikzpicture}
\end{document}

Output

Alternatively, just use anchor like how it's normally done.

\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (4,0);
\draw (A)--node[below]{4}(B);
\node [anchor=0, outer sep=0.2cm] at (A){A};
\node [anchor=180, outer sep=0.2cm] at (B){B};
\end{tikzpicture}
\hskip5mm
\begin{tikzpicture}[scale=0.1]
\coordinate (A) at (0,0);
\coordinate (B) at (40,0);
\draw (A)--node[below]{40}(B);
\node [anchor=0, outer sep=0.2cm] at (A){A};
\node [anchor=180, outer sep=0.2cm] at (B){B};
\end{tikzpicture}

And outer sep to control the spacing.

Output

user202729
  • 7,143