samcarter
I have a line that should scale with the rest of the picture. For "normal" scaling, e.g. scale=... the following trick works fine, but as soon as the coordinate system of the tikzpicture is changed, e.g. [x=1mm,y=1mm], it no longer works.

Is there any possibility to take the coordinate system into account when scaling the line?


\documentclass{standalone}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\pgfgettransformentries{\tmpscaleA}{\tmpscaleB}{\tmpscaleC}{\tmpscaleD}{\tmp}{\tmp}%
\pgfmathsetmacro{\scalingfactor}{sqrt(abs(\tmpscaleA*\tmpscaleD-\tmpscaleB*\tmpscaleC))}%
\draw[line width=\scalingfactor*0.4pt] (0.145, 1.38) arc [start angle=-20, end angle=-160, radius=0.16];
\end{tikzpicture}

\begin{tikzpicture}[scale=0.1]
\pgfgettransformentries{\tmpscaleA}{\tmpscaleB}{\tmpscaleC}{\tmpscaleD}{\tmp}{\tmp}%
\pgfmathsetmacro{\scalingfactor}{sqrt(abs(\tmpscaleA*\tmpscaleD-\tmpscaleB*\tmpscaleC))}%
\draw[line width=\scalingfactor*0.4pt] (0.145, 1.38) arc [start angle=-20, end angle=-160, radius=0.16];
\end{tikzpicture}

\begin{tikzpicture}[x=1mm,y=1mm]
\pgfgettransformentries{\tmpscaleA}{\tmpscaleB}{\tmpscaleC}{\tmpscaleD}{\tmp}{\tmp}%
\pgfmathsetmacro{\scalingfactor}{sqrt(abs(\tmpscaleA*\tmpscaleD-\tmpscaleB*\tmpscaleC))}%
\draw[line width=\scalingfactor*0.4pt] (0.145, 1.38) arc [start angle=-20, end angle=-160, radius=0.16];
\end{tikzpicture}

\end{document}


![Screen Shot 2020-03-16 at 14.40.59.png](/image?hash=e3439f38d6443338731018c4c4f4c632be12f6e907c5c9b9897ff703100694a6)
marmot
This answer assumes that you are dealing with 2d pictures only. Then the basis vectors are stored in e_x=(\pgf@xx,\pgf@xy) and e_y=(\pgf@yx,\pgf@yy). This means that there is a second Jacobian, on top of the one you are computing. The following code defines a key scale line widths which uses both Jacobians to scale the line width.


\documentclass{article}
\usepackage{tikz}
\makeatletter
\tikzset{scale line widths/.code={%
\pgfgettransformentries{\tmpa}{\tmpb}{\tmpc}{\tmpd}{\tmp}{\tmp}%
\pgfmathsetmacro{\myJacobian}{sqrt(abs(\tmpa*\tmpd-\tmpb*\tmpc))*%
sqrt(abs((\pgf@xx/1cm)*(\pgf@yy/1cm)-(\pgf@xy/1cm)*(\pgf@yx/1cm)))}%
\pgfmathsetlength\pgflinewidth{\myJacobian*0.4pt}%
\pgfmathsetmacro{\my@lw}{\myJacobian*##1}%
\pgfmathsetlength\pgflinewidth{\my@lw pt}}%
\tikzset{thin}}%
}
\makeatother
\newcommand\CenterObject[1]{$\vcenter{\hbox{#1}}$}
\begin{document}

\subsubsection*{Original}
\begin{tikzpicture}
\draw (0,0) rectangle (1,1);
\draw[thick] (0,0) -- (0,1);
\draw[line width=2pt] (1,0) -- (1,1);
\begin{scope}[scale=2]
\draw[line width=2pt] (1,0) -- (1,0.5);
\end{scope}
\end{tikzpicture}

\subsubsection*{Just scale or change the basis vectors}
\paragraph{scale:}\CenterObject{\begin{tikzpicture}[scale=4]
\draw (0,0) rectangle (1,1);
\draw[thick] (0,0) -- (0,1);
\draw[line width=2pt] (1,0) -- (1,1);
\begin{scope}[scale=2]
\draw[line width=2pt] (1,0) -- (1,0.5);
\end{scope}
\end{tikzpicture}}

\paragraph{change basis vectors:}\CenterObject{\begin{tikzpicture}[x=5mm,y=5mm]
\draw (0,0) rectangle (1,1);
\draw[thick] (0,0) -- (0,1);
\draw[line width=2pt] (1,0) -- (1,1);
\begin{scope}[scale=2]
\draw[line width=2pt] (1,0) -- (1,0.5);
\end{scope}
\end{tikzpicture}}

\subsubsection*{Scale, change the basis vectors and use \texttt{scale line widths}}
\paragraph{scale and use \texttt{scale line widths}:}\CenterObject{\begin{tikzpicture}[scale=4,scale line widths]
\draw (0,0) rectangle (1,1);
\draw[thick] (0,0) -- (0,1);
\draw[line width=2pt] (1,0) -- (1,1);
\begin{scope}[scale=2]
\draw[line width=2pt] (1,0) -- (1,0.5);
\end{scope}
\end{tikzpicture}}

\paragraph{change basis vectors and use \texttt{scale line widths}:}%
\CenterObject{\begin{tikzpicture}[x=5mm,y=5mm,scale line widths]
\draw (0,0) rectangle (1,1);
\draw[thick] (0,0) -- (0,1);
\draw[line width=2pt] (1,0) -- (1,1);
\begin{scope}[scale=2]
\draw[line width=2pt] (1,0) -- (1,0.5);
\end{scope}
\end{tikzpicture}}

\paragraph{combining the two:}\CenterObject{\begin{tikzpicture}[scale=2,x=5mm,y=5mm,scale line widths]
\draw (0,0) rectangle (1,1);
\draw[thick] (0,0) -- (0,1);
\draw[line width=2pt] (1,0) -- (1,1);
\begin{scope}[scale=2]
\draw[line width=2pt] (1,0) -- (1,0.5);
\end{scope}
\end{tikzpicture}}

\end{document}


![Screen Shot 2020-03-16 at 7.51.21 AM.png](/image?hash=df0f0c5497c5f2f002a1c79069dc638653ceb1b90c74498f1a623b51cdc0cb62)

Notice that the line in a scope with an additional scale factor has been added on purpose to hint at possible pitfalls.

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.