Using font-relative distances in tikzpictures

Your 1em is being converted at read time into the coordinate system, which is then affected by the picture scale. You can use reset cm to locally reset the coordinate transformation matrix, locally cancelling the picture scale effect.

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[baseline]
    \draw (0,0)  --(1,0) coordinate(A) -- (2,0);
    \path (A) ++(0,1em) coordinate(B);
    \node [anchor=base] at (B) {$R_g$};
\end{tikzpicture}
\begin{tikzpicture}[baseline, scale=2]
  \draw (0,0)  --(0.5,0) coordinate(A) -- (1,0);
  \path[reset cm] (A) ++(0,1em) coordinate(B);
  \node [anchor=base] at (B) {$R_g$};
\end{tikzpicture}
\begin{tikzpicture}[baseline, scale=0.01]
  \draw (0,0)  --(100,0) coordinate(A) -- (200,0);
  \path[reset cm] (A) ++(0,1em) coordinate(B);
  \node [anchor=base] at (B) {$R_g$};
\end{tikzpicture}
\end{document}

Resetting the scale locally is one option (see Marsden's answer), but there is a better way to go about things.

With scale=factor, all coordinates are scaled by factor, regardless of their dimension (while the unit vectors are unchanged, as you discovered). Instead, redefine the length of the unit vectors according to the desired scaling. For example, with y={(0cm,2cm)}, while (0,0) -- (1,0) will be twice the usual distance, coordinates with dimensions, such as (0,1ex), will be unchanged:

\begin{document}
\begin{tikzpicture}[baseline]
    \draw (0,0)  --(1,0) coordinate(A) -- (2,0);
    \path (A) ++(0,1ex) coordinate(B);
    \node [anchor=base] at (B) {$R_g$};
    \showat{1,-1cm}
\end{tikzpicture}
\begin{tikzpicture}[baseline, x={(2cm,0cm)},y={(0cm,2cm)}]
    % Identical code to previous picture:
    \draw (0,0)  --(1,0) coordinate(A) -- (2,0);
    \path[scale=1] (A) ++(0,1ex) coordinate(B);
    \node [anchor=base] at (B) {$R_g$};
    \showat{1,-1cm}
\end{tikzpicture}
\end{document}

Output:

output

Note how the distance between A and B is unchanged, while \the\pgf@yy has doubled, as expected.


Why don't you place B as itself a node (with relative placement)?

That would solve all your problems.

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\newdimen\mydimA\newdimen\mydimB
\makeatletter
\newcommand{\showat}[1]{%
    \pgfextracty\mydimA{\pgfpointanchor{A}{center}}
    \pgfextracty\mydimB{\pgfpointanchor{B}{center}}
    \node[red, font=\tiny,  align=left] at(#1) {Before \the\mydimA \\ After \the\mydimB \\
    Scale y \the\pgf@yy};
}
\makeatother
\begin{document}
\tikzset{node distance=1ex}
\begin{tikzpicture}[baseline]
    \draw (0,0)  --(1,0) coordinate(A) -- (2,0);
    \node[above=of A,anchor=base](B){$R_g$};
    \showat{1,-1}
\end{tikzpicture}
\begin{tikzpicture}[baseline, scale=2]
    \draw (0,0)  --(0.5,0) coordinate(A) -- (1,0);
    %\path (A) ++(0,1ex) coordinate(B)node{y};
    \node[above=of A,anchor=base] (B) {$R_g$};
    \showat{0.5,-0.5}
\end{tikzpicture}
\begin{tikzpicture}[baseline, scale=0.01]
    \draw (0,0)  --(100,0) coordinate(A) -- (200,0);
    %\path (A) ++(0,1ex) coordinate(B)node{y};
    \node[above=of A,anchor=base] (B) {$R_g$};
    \showat{100,-100}
\end{tikzpicture}
\end{document}

screenshot

Tags:

Tikz Pgf