为什么缩放 tikz 图片会导致变换画布范围内的所有内容消失?

为什么缩放 tikz 图片会导致变换画布范围内的所有内容消失?

我画了一幅 tikz 图片,但是使用缩放图片后\begin{tikzpicture}[scale=200/(\hsize-24)],变换画布范围内的所有内容都消失了。有人可以解释一下吗?

这是原始的 tikz 图片

\documentclass[tikz, border=12]{standalone}

\usepackage{tikz, tkz-euclide, pgfmath, pstricks}
\usetikzlibrary{intersections, decorations.markings, angles,
quotes, calc, arrows, arrows.meta}
\usetkzobj{all}
%
\definecolor{blue}{RGB}{0,51,255}
\definecolor{green}{RGB}{0,153,0}
\definecolor{blue1}{RGB}{174,214,241}
\definecolor{blue}{RGB}{161,207,223}
\definecolor{yellow}{RGB}{255,220,0}
\colorlet{dcolor}{blue}
%
\begin{document}
\begin{tikzpicture}
\pgfgettransformentries{\mya}{\myb}{\myc}{\myd}{\mys}{\myt}
\pgfmathsetmacro{\preserve}{1/\mya}
\begin{scope}[>={Stealth[scale=1.2]} , thick,rotate=0 ] 

%
\newcommand\height{10}
%
\newcommand\Ac{-4}
%
\newcommand\Bc{27.46586300953384}
%
\pgfmathsetmacro{\distance}{\Bc+\Ac}
\draw (0,-0.2) node [color=black,fill=white,below] {$Q$};
\draw [fill=black] (\Ac-0.1*\distance,0)--(\Bc+0.1*\distance,0)--(\Bc+0.1*\distance,0.01*\height)--(\Ac-0.1*\distance,0.01*\height)--cycle;
\draw [fill=black] (\Ac-0.1*\distance,\height)--(\Bc+0.1*\distance,\height)--(\Bc+0.1*\distance,1.01*\height)--(\Ac-0.1*\distance,1.01*\height)--cycle;
\draw [black,densely dashed](0,0)--(0,\height);


%
\coordinate (pulleyCenter) at (0,0.9*\height);
%
\pgfmathsetmacro{\pulleyRadius}{0.0375*\height}

\draw (0,0.9*\height-\pulleyRadius) node [color=black,fill=white,below] {$P$};

\draw [line width = 0.2*\distance,line cap=round](0,0.9*\height)--(0,0.995*\height);
\draw [line width = 0.2*\distance]([shift={(0,\height)}]0:-0.025*\height) arc[radius=0.025*\height, start angle=180, end angle= 360];

\pgfmathsetmacro{\ax}{\Bc-0.04*\distance}
\pgfmathsetmacro{\bx}{\Bc+0.04*\distance}
\pgfmathsetmacro{\lowerwidth}{\bx-\ax}
\pgfmathsetmacro{\aby}{0.2+0.01*\height}
\pgfmathsetmacro{\dx}{\Bc-0.08*\distance}
\pgfmathsetmacro{\cx}{\Bc+0.08*\distance}
\pgfmathsetmacro{\upperwidth}{\cx-\dx}
\pgfmathsetmacro{\cartheight}{1.25*\lowerwidth}
\pgfmathsetmacro{\dcy}{\aby+\cartheight}
\coordinate(a) at (\ax,\aby);
\coordinate(b) at (\bx,\aby);
\coordinate(d) at (\dx,\dcy);
\coordinate(c) at (\cx,\dcy);


\pgfmathsetmacro{\adx}{\Bc-0.06*\distance}
\pgfmathsetmacro{\ady}{\aby+0.5*\cartheight}
\coordinate (c1) at (\adx,\ady);
\draw [gray!120,line width = 0.01*\height cm,line cap=round](c1)--++(180:0.01*\Bc) coordinate (e1);


\coordinate (c2) at (\Bc+0.06*\distance,\ady);
\draw [gray!120,line width = 0.01*\height cm,line cap=round](c2)--++(0:0.01*\Bc) coordinate (e2);


\draw [line width = 0.005*\height cm,blue!200,fill=blue](a)--(b)--(c)--(d)--cycle;

\tkzTangent[from with R=e1](pulleyCenter,\pulleyRadius cm) \tkzGetPoints{D1}{D2}

\draw[yellow, line width = 0.010*\height cm,line cap =round](e1)--(D1);
\draw [gray!120,line width = 0.01*\height cm,line cap=round](c1)--(e1);
\draw [line width = 0.005*\height cm,blue!200,fill=blue](a)--(b)--(c)--(d)--cycle;

\pgfmathsetmacro{\wheelax}{0.25*\lowerwidth+\ax}
\pgfmathsetmacro{\wheelbx}{0.75*\lowerwidth+\ax}
\draw[black,fill=black](\wheelax,\aby)circle(\aby-0.011*\height);
\draw[black,fill=gray](\wheelax,\aby)circle(0.75*\aby-0.75*0.011*\height);

\draw[black,fill=black](\wheelbx,\aby)circle(\aby-0.011*\height);
\draw[black,fill=gray](\wheelbx,\aby)circle(0.75*\aby-0.75*0.011*\height);

\draw (\Bc,\ady) node [color=black] {$A$};


\pgfmathsetmacro{\shift}{\Ac-\Bc}
\begin{scope}[transform canvas={xshift=\shift cm}]
\draw [gray!120,line width = 0.01*\height cm,line cap=round](c1)--(e1);
\draw [gray!120,line width = 0.01*\height cm,line cap=round](c2)--(e2);
\coordinate (pulleyCenter) at (-1*\shift,0.9*\height);
\tkzTangent[from with R=e2](pulleyCenter,\pulleyRadius cm) \tkzGetPoints{D1}{D2}
\draw[yellow, line width = 0.01*\height cm,line cap =round](e2)--(D2);
\draw[gray!120,line width = 0.01*\height cm,line cap=round](c2)--(e2);
\draw [line width = 0.005*\height cm,blue!200,fill=blue](a)--(b)--(c)--(d)--cycle;
\draw[black,fill=black](\wheelax,\aby)circle(\aby-0.011*\height);
\draw[black,fill=gray](\wheelax,\aby)circle(0.75*\aby-0.75*0.011*\height);

\draw[black,fill=black](\wheelbx,\aby)circle(\aby-0.011*\height);
\draw[black,fill=gray](\wheelbx,\aby)circle(0.75*\aby-0.75*0.011*\height);
\draw (\Bc,\ady) node [color=black] {$B$};

\draw [gray,fill=gray!50,line width = 0.005*\height cm](pulleyCenter) circle(\pulleyRadius);
\end{scope}



\end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

这是使用\begin{tikzpicture}[scale=200/(\hsize-24)]

\documentclass[tikz, border=12]{standalone}

\usepackage{tikz, tkz-euclide, pgfmath, pstricks}
\usetikzlibrary{intersections, decorations.markings, angles,
quotes, calc, arrows, arrows.meta}
\usetkzobj{all}
%
\definecolor{blue}{RGB}{0,51,255}
\definecolor{green}{RGB}{0,153,0}
\definecolor{blue1}{RGB}{174,214,241}
\definecolor{blue}{RGB}{161,207,223}
\definecolor{yellow}{RGB}{255,220,0}
\colorlet{dcolor}{blue}
%
\begin{document}
\begin{tikzpicture}[scale=200/(\hsize-24)]

\pgfgettransformentries{\mya}{\myb}{\myc}{\myd}{\mys}{\myt}
\pgfmathsetmacro{\preserve}{1/\mya}
\begin{scope}[>={Stealth[scale=1.2]} , thick,rotate=0 ] 

%
\newcommand\height{10}
%
\newcommand\Ac{-4}
%
\newcommand\Bc{27.46586300953384}
%
\pgfmathsetmacro{\distance}{\Bc+\Ac}
\draw (0,-0.2) node [color=black,fill=white,below] {$Q$};
\draw [fill=black] (\Ac-0.1*\distance,0)--(\Bc+0.1*\distance,0)--(\Bc+0.1*\distance,0.01*\height)--(\Ac-0.1*\distance,0.01*\height)--cycle;
\draw [fill=black] (\Ac-0.1*\distance,\height)--(\Bc+0.1*\distance,\height)--(\Bc+0.1*\distance,1.01*\height)--(\Ac-0.1*\distance,1.01*\height)--cycle;
\draw [black,densely dashed](0,0)--(0,\height);


%
\coordinate (pulleyCenter) at (0,0.9*\height);
%
\pgfmathsetmacro{\pulleyRadius}{0.0375*\height}

\draw (0,0.9*\height-\pulleyRadius) node [color=black,fill=white,below] {$P$};

\draw [line width = 0.2*\distance,line cap=round](0,0.9*\height)--(0,0.995*\height);
\draw [line width = 0.2*\distance]([shift={(0,\height)}]0:-0.025*\height) arc[radius=0.025*\height, start angle=180, end angle= 360];

\pgfmathsetmacro{\ax}{\Bc-0.04*\distance}
\pgfmathsetmacro{\bx}{\Bc+0.04*\distance}
\pgfmathsetmacro{\lowerwidth}{\bx-\ax}
\pgfmathsetmacro{\aby}{0.2+0.01*\height}
\pgfmathsetmacro{\dx}{\Bc-0.08*\distance}
\pgfmathsetmacro{\cx}{\Bc+0.08*\distance}
\pgfmathsetmacro{\upperwidth}{\cx-\dx}
\pgfmathsetmacro{\cartheight}{1.25*\lowerwidth}
\pgfmathsetmacro{\dcy}{\aby+\cartheight}
\coordinate(a) at (\ax,\aby);
\coordinate(b) at (\bx,\aby);
\coordinate(d) at (\dx,\dcy);
\coordinate(c) at (\cx,\dcy);


\pgfmathsetmacro{\adx}{\Bc-0.06*\distance}
\pgfmathsetmacro{\ady}{\aby+0.5*\cartheight}
\coordinate (c1) at (\adx,\ady);
\draw [gray!120,line width = 0.01*\height cm,line cap=round](c1)--++(180:0.01*\Bc) coordinate (e1);


\coordinate (c2) at (\Bc+0.06*\distance,\ady);
\draw [gray!120,line width = 0.01*\height cm,line cap=round](c2)--++(0:0.01*\Bc) coordinate (e2);


\draw [line width = 0.005*\height cm,blue!200,fill=blue](a)--(b)--(c)--(d)--cycle;

\tkzTangent[from with R=e1](pulleyCenter,\pulleyRadius cm) \tkzGetPoints{D1}{D2}

\draw[yellow, line width = 0.010*\height cm,line cap =round](e1)--(D1);
\draw [gray!120,line width = 0.01*\height cm,line cap=round](c1)--(e1);
\draw [line width = 0.005*\height cm,blue!200,fill=blue](a)--(b)--(c)--(d)--cycle;

\pgfmathsetmacro{\wheelax}{0.25*\lowerwidth+\ax}
\pgfmathsetmacro{\wheelbx}{0.75*\lowerwidth+\ax}
\draw[black,fill=black](\wheelax,\aby)circle(\aby-0.011*\height);
\draw[black,fill=gray](\wheelax,\aby)circle(0.75*\aby-0.75*0.011*\height);

\draw[black,fill=black](\wheelbx,\aby)circle(\aby-0.011*\height);
\draw[black,fill=gray](\wheelbx,\aby)circle(0.75*\aby-0.75*0.011*\height);

\draw (\Bc,\ady) node [color=black] {$A$};


\pgfmathsetmacro{\shift}{\Ac-\Bc}
\begin{scope}[transform canvas={xshift=\shift cm}]
\draw [gray!120,line width = 0.01*\height cm,line cap=round](c1)--(e1);
\draw [gray!120,line width = 0.01*\height cm,line cap=round](c2)--(e2);
\coordinate (pulleyCenter) at (-1*\shift,0.9*\height);
\tkzTangent[from with R=e2](pulleyCenter,\pulleyRadius cm) \tkzGetPoints{D1}{D2}
\draw[yellow, line width = 0.01*\height cm,line cap =round](e2)--(D2);
\draw[gray!120,line width = 0.01*\height cm,line cap=round](c2)--(e2);
\draw [line width = 0.005*\height cm,blue!200,fill=blue](a)--(b)--(c)--(d)--cycle;
\draw[black,fill=black](\wheelax,\aby)circle(\aby-0.011*\height);
\draw[black,fill=gray](\wheelax,\aby)circle(0.75*\aby-0.75*0.011*\height);

\draw[black,fill=black](\wheelbx,\aby)circle(\aby-0.011*\height);
\draw[black,fill=gray](\wheelbx,\aby)circle(0.75*\aby-0.75*0.011*\height);
\draw (\Bc,\ady) node [color=black] {$B$};

\draw [gray,fill=gray!50,line width = 0.005*\height cm](pulleyCenter) circle(\pulleyRadius);
\end{scope}



\end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

tikz 图片的缩放不适用于变换画布内的 xshift,所以我所要做的就是将缩放应用于变换画布外部的移位,然后在画布变换中使用缩放值。所以我用了这个

\pgfmathsetmacro{\shift}{\Ac-\Bc}
\pgfmathsetmacro{\shifta}{200/(\hsize-24)*\shift}
\begin{scope}[transform canvas={xshift=\shifta cm}]

而不是这个

\pgfmathsetmacro{\shift}{\Ac-\Bc}
\begin{scope}[transform canvas={xshift=\shift cm}]

其余代码都是一样的。

相关内容