4

I have some tikzpictures that were designed for a4 paper and that should now be used on an a0 poster. All the coordinates in the picture can be scaled up with the option scale=4, but this leaves the line widths untouched.

Can I scale the line widths, even those set directly and not using styles like thin, in the entire tikzpicture? Text contained in the picture should not be scaled by this.

  • Please not that "you could just make an a4 poster and print it on a0", while being a workaround, is not an answer to this question. – schtandard Nov 6 '19 at 23:19
  • 2
    Try \def\tikz@semiaddlinewidth#1{\tikz@addoption{\pgfsetlinewidth{4*#1}}\pgfmathsetlength\pgflinewidth{4*#1}}. It should work in most of the cases, but probably not all of them. TikZ/PGF uses a TeX dimen to store the line width, so there's no way to step in and scale the line width by some value in a TeX assignment like \pgflinewidth=1pt. If the above fails somewhere, then the solution will probably be to scale the line width directly in the driver code. – Phelype Oleinik Nov 6 '19 at 23:43
10

This is similar to Phelype's comment (which I see just now) but it uses the (square root of the) Jacobian of the current transformation matrix to determine the scale factor.

\documentclass{article}
\usepackage{tikz}
% Jacobians have already been used in https://tex.stackexchange.com/q/86897/138900
% https://tex.stackexchange.com/a/496418 and https://tex.stackexchange.com/a/506249/194703
\makeatletter
\tikzset{scale line widths/.style={%
/utils/exec=\pgfgettransformentries{\tmpa}{\tmpb}{\tmpc}{\tmpd}{\tmp}{\tmp}%
\pgfmathsetmacro{\myJacobian}{sqrt(abs(\tmpa*\tmpd-\tmpb*\tmpc))}%
\pgfmathsetlength\pgflinewidth{\myJacobian*0.4pt}%
\def\tikz@semiaddlinewidth##1{\pgfmathsetmacro{\my@lw}{\myJacobian*##1}%
\tikz@addoption{\pgfsetlinewidth{\my@lw pt}}\pgfmathsetlength\pgflinewidth{\my@lw pt}},%
thin}}
\makeatother
\begin{document}
\subsubsection*{Original}
\begin{tikzpicture}
 \draw (0,0) rectangle (1,1);
 \draw[thick] (0,0) -- (1,1);
 \draw[line width=2pt] (0,1) -- (1,0);
\end{tikzpicture}

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

\subsubsection*{Scale and \texttt{scale line widths}}
\begin{tikzpicture}[scale=4,scale line widths]
 \draw (0,0) rectangle (1,1);
 \draw[thick] (0,0) -- (1,1);
 \draw[line width=2pt] (0,1) -- (1,0);
 \typeout{\the\pgflinewidth}
\end{tikzpicture}
\end{document}

enter image description here

(Yes, I also see that the line joins are not nice, but this is not the point of this post. ;-)

And this is a version that locally retrieves the scale factor.

\documentclass{article}
\usepackage{tikz}
% Jacobians have already been used in https://tex.stackexchange.com/q/86897/138900
% https://tex.stackexchange.com/a/496418 and https://tex.stackexchange.com/a/506249/194703
\makeatletter
\tikzset{scale line widths/.style={%
/utils/exec=\def\tikz@semiaddlinewidth##1{%
\pgfgettransformentries{\tmpa}{\tmpb}{\tmpc}{\tmpd}{\tmp}{\tmp}%
\pgfmathsetmacro{\myJacobian}{sqrt(abs(\tmpa*\tmpd-\tmpb*\tmpc))}%
\pgfmathsetlength\pgflinewidth{\myJacobian*0.4pt}%
\pgfmathsetmacro{\my@lw}{\myJacobian*##1}%
\tikz@addoption{\pgfsetlinewidth{\my@lw pt}}\pgfmathsetlength\pgflinewidth{\my@lw pt}},%
thin}}
\makeatother
\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}
\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}

\subsubsection*{Scale and \texttt{scale line widths}}
\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}
\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.