我正在尝试将我创建的饼图的标题与tikz
pgf-pie
包对齐。目标是让多个饼图在一行中彼此相邻。
到目前为止,我尝试创建一个figure
环境,将tikz
包含的环境pie-chart
放在图中并添加标题。但是标题没有集中在饼图下方,而是奇怪地集中在页面中央。如果我\centering
在环境中使用该命令figure
,那么整个图形/饼图将集中在页面中央,而不是左对齐。
那么,如何将标题与饼图的中心对齐而不是与页面对齐?
看起来应该如何
外观
我的最小工作示例如下所示:
\documentclass[11pt]{scrartcl}
\RequirePackage{tikz}
\RequirePackage{pgf-pie}
\begin{document}
\begin{figure}[h]
%\centering
\begin{tikzpicture} % Tikz environment
\pie[rotate=270, radius=2]
{46/Germany, 21/Austria and Switzerland, 25/Europe, 8/Asia and America}
\end{tikzpicture}
\caption{Revenue by Geographic}
\end{figure}
\end{document}
答案1
根据您的需要,这里有两种解决方案:
- 使用小页面
- 使用子图
所以
\documentclass[11pt]{scrartcl}
\RequirePackage{tikz}
\RequirePackage{pgf-pie}
\usepackage{subcaption}
\usepackage{lipsum}
\begin{document}
\lipsum[1]
\noindent\fbox{\begin{minipage}{.45\linewidth}
\centering
\begin{tikzpicture}[scale=.7, every node/.style={scale=0.7}] % Tikz environment
\pie[rotate=270, radius=2]
{46/Germany, 21/Austria and Switzerland, 25/Europe, 8/Asia and America}
\end{tikzpicture}
\captionof{figure}{Revenue by Geographic}
\end{minipage}}
\hfill
\fbox{\begin{minipage}{.45\linewidth}
\centering
\begin{tikzpicture}[scale=.7, every node/.style={scale=0.7}] % Tikz environment
\pie[rotate=270, radius=2]
{46/Germany, 21/Austria and Switzerland, 25/Europe, 8/Asia and America}
\end{tikzpicture}
\captionof{figure}{Revenue by Geographic}
\end{minipage}}
\begin{figure}[h!]
\fbox{\begin{subfigure}[b]{.45\linewidth}
\centering\begin{tikzpicture}[scale=.7, every node/.style={scale=0.7}] % Tikz environment
\pie[rotate=270, radius=2]
{46/Germany, 21/Austria and Switzerland, 25/Europe, 8/Asia and America}
\end{tikzpicture}
\caption{A subfigure}\label{fig:1a}
\end{subfigure}}%
\hfill
\fbox{\begin{subfigure}[b]{.45\linewidth}
\centering\begin{tikzpicture}[scale=.7, every node/.style={scale=0.7}] % Tikz environment
\pie[rotate=270, radius=2]
{46/Germany, 21/Austria and Switzerland, 25/Europe, 8/Asia and America}
\end{tikzpicture}
\caption{Another subfigure}\label{fig:1b}
\end{subfigure}}
\caption{A figure}\label{fig:1}
\end{figure}
\lipsum[2]
\end{document}
答案2
饼图的中心 (0,0) 通常不是边界框的中心。如果要将标题与饼图对齐,则需要使边界框关于原点对称。
我添加了一个覆盖层来准确显示中心。
\documentclass[11pt]{scrartcl}
\RequirePackage{tikz}
\RequirePackage{pgf-pie}
\begin{document}
\begin{figure}[h]
\centering
\begin{tikzpicture} % Tikz environment
\pie[rotate=270, radius=2]
{46/Germany, 21/Austria and Switzerland, 25/Europe, 8/Asia and America}
\path (current bounding box.south west);
\pgfgetlastxy{\xleft}{\ybottom}%
\path (current bounding box.north east);
\pgfgetlastxy{\xright}{\ytop}%
\path (-\xleft,-\ybottom) (-\xright,-\ytop);% symmetrize bounding box
\end{tikzpicture}
\caption{Revenue by Geographic}
\smash{\rule{0.5pt}{2in}}% overlay line through center (remove)
\end{figure}
\end{document}