为什么旋转tikzpicture
圆圈会改变边界框的高度和宽度?有没有办法不产生这种效果?
下面的 MWE 得出:
代码:
\documentclass{article}
\usepackage{tikz}
\usepackage{xstring}
\newcommand*{\DrawBoundingBox}[1][]{%
\draw [red]
([shift={(-5pt,-5pt)}]current bounding box.south west)
rectangle
([shift={(5pt,+5pt)}]current bounding box.north east);
% https://tex.stackexchange.com/questions/418499/
% align-tikzpictures-at-internal-bounding-box-with-text-below-it
\coordinate (X) at (current bounding box.south);
\tikzset{baseline={(X)}} % X is the alignment point
\IfStrEq{#1}{}{}{%
\node [below, anchor=north, align=center,
baseline=0pt, thin, shift only, solid,
]
at (current bounding box.south)
{#1\strut};
}%
}
\newcommand*{\MyCircle}[2][]{%
%% #1 = tikz picture options
%% #2 = text
\begin{tikzpicture}
\draw [fill=yellow!20, draw=black, #1] (0,0) circle (1.0cm);
\DrawBoundingBox[#2]
\end{tikzpicture}%
}
\begin{document}
\section{Height Changes}
\noindent
\MyCircle[fill=green!25]{rotate=0}~%
\MyCircle[fill=cyan!40, rotate=20]{rotate=20}~%
\MyCircle[fill=orange!40, rotate=60]{rotate=60}%
\section{Width Changes}
\par\noindent\MyCircle[fill=green!25]{rotate=0}%
\par\noindent\MyCircle[fill=cyan!40, rotate=20]{rotate=20}%
\end{document}
答案1
一个圆由四条贝塞尔曲线绘制。贝塞尔曲线的控制点用于计算边界框:
\documentclass[tikz]{standalone}
\usetikzlibrary{decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}[>=stealth, every node/.style={midway, sloped, font=\tiny},
decoration={show path construction,
moveto code={},
lineto code={},
curveto code={
\fill[red] (\tikzinputsegmentsupporta) circle(.5pt);
\fill[red] (\tikzinputsegmentsupportb) circle(.5pt);
\fill[blue] (\tikzinputsegmentfirst) circle(.5pt);
\fill[blue] (\tikzinputsegmentlast) circle(.5pt);
\draw[blue,->] (\tikzinputsegmentfirst) -- (\tikzinputsegmentsupporta);
\draw[blue,->] (\tikzinputsegmentlast) -- (\tikzinputsegmentsupportb);
\draw [black] (\tikzinputsegmentfirst) .. controls
(\tikzinputsegmentsupporta) and (\tikzinputsegmentsupportb)
..(\tikzinputsegmentlast);
},
closepath code={},
}]
\draw [help lines] grid (6,3);
\begin{scope}[local bounding box=lbb]
\path [decorate,rotate around={0:(1.5,1.5)}] (1.5,1.5) circle(1);
\draw[red] (lbb.north west) rectangle (lbb.south east);
\end{scope}
\begin{scope}[local bounding box=lbb]
\path [decorate,rotate around={30:(4.5,1.5)}] (4.5,1.5) circle(1);
\draw[red] (lbb.north west) rectangle (lbb.south east);
\end{scope}
\end{tikzpicture}
\end{document}
答案2
我猜你没有使用合适的方法来旋转圆圈。如果你使用transform canvas
,则不会出现任何问题。更新我忘了放到transform canvas
第三个圈里了。
\documentclass{article}
\usepackage{tikz}
\usepackage{xstring}
\newcommand*{\DrawBoundingBox}[1][]{%
\draw [red]
([shift={(-5pt,-5pt)}]current bounding box.south west)
rectangle
([shift={(5pt,+5pt)}]current bounding box.north east);
% https://tex.stackexchange.com/questions/418499/
% align-tikzpictures-at-internal-bounding-box-with-text-below-it
\coordinate (X) at (current bounding box.south);
\tikzset{baseline={(X)}} % X is the alignment point
\IfStrEq{#1}{}{}{%
\node [below, anchor=north, align=center,
baseline=0pt, thin, shift only, solid,
]
at (current bounding box.south)
{#1\strut};
}%
}
\newcommand*{\MyCircle}[2][]{%
%% #1 = tikz picture options
%% #2 = text
\begin{tikzpicture}
\draw [fill=yellow!20, draw=black, #1] (0,0) circle (1.0cm);
\draw [#1] (-1,0) -- (1,0); % added to see that the transformatio does
% something
\DrawBoundingBox[#2]
\end{tikzpicture}%
}
\begin{document}
\section{No Height Changes}
\noindent
\MyCircle[fill=green!25]{rotate=0}~%
\MyCircle[fill=cyan!40, transform canvas={rotate=20}]{rotate=20}~%
\MyCircle[fill=orange!40, transform canvas={rotate=60}]{rotate=60}%
\section{No Width Changes}
\par\noindent\MyCircle[fill=green!25]{rotate=0}%
\par\noindent\MyCircle[fill=cyan!40, transform canvas={rotate=20}]{rotate=20}%
\end{document}
(我添加了一行来表明这种变换确实旋转了圆圈,否则很难看出这一点 ;-)