将 tickzpicture 置于子浮点数的中心

将 tickzpicture 置于子浮点数的中心

我的饼图中有此代码,我正在尝试将子浮点数和每个饼图的标签置于中心。

这是我的代码:

\documentclass[oneside,12pt]{scrbook}

\usepackage{tikz}%esquema
\usepackage{subfig}

\begin{document}


\newcommand{\slicepie}[4]{
\pgfmathsetmacro\midangle{0.5*#1+0.5*#2}
% slice
\draw[thick,fill=black!10] (0,0) -- (#1:1) arc (#1:#2:1) -- cycle;
\ifnum\p<11
\def\plabel{#3 (\p\%)}%
\def\slabel{}%
\else
\def\plabel{#3}%
\def\slabel{\p\%}%
\fi
%outer label
\node  [label={[name=l \j]\midangle:\plabel}, yshift=#4] at (\midangle:1.15) {};
%inner label
\pgfmathsetmacro\innerpos{max(min((#2-#1-10)/110*(-0.3),0),-.5) +.8}
\pgfmathsetmacro\labelanchor{ ( \midangle < 90 ) ? "south west" : ( ( \midangle < 180 ) ? "east" : ( ( \midangle < 270 ) ? "north" : "west" ) ) }
\node (slabel \j) at (\midangle:\innerpos) {\slabel};
\draw (slabel \j) -- (l \j.\labelanchor) ;

 }

 \begin{center}
 \begin{figure}[!ht]    

\subfloat[Strategy no. 2]{\label{fig:in}
    \begin{tikzpicture}[thick,scale=0.76, every node/.style={transform shape}]
    \centering
    \newcounter{z}
    \newcounter{g}
    \foreach \p/\t/\n [count=\j] in {
        83/Global equities/-10pt,           
        9/Absolute Return Bonds/10pt,
        8/Others\footnote\smallskip/10pt}
    {
        \setcounter{z}{\value{g}}
        \addtocounter{g}{\p}
        \slicepie{\thez/100*360}
        {\theg/100*360} 
        {\t}
        {\n}
    }

    \end{tikzpicture}
}

\subfloat[Strategy no. 3]{\label{fig:in}
    \begin{tikzpicture}[thick,scale=0.76, every node/.style={transform shape}]
    \centering
    \newcounter{q}
    \newcounter{w}
    \foreach \p/\t/\n [count=\j] in {
        35/Absolute Return Bonds/0pt,
        7/Euro Corporate bonds ($>$10 years)/0pt,
        4/Eurozone Equities/0pt,
        4/Fixed Interest euro govt Bonds ($>$10years)/-5pt,
        43/Global equities/0pt,
        3/Index-Linked euro govts (AS)/10pt,
        8/Others\footnote\smallskip/15pt}
    {
        \setcounter{q}{\value{w}}
        \addtocounter{w}{\p}
        \slicepie{\theq/100*360}
        {\thew/100*360} 
        {\t}
        {\n}
    }

    \end{tikzpicture}
}

\subfloat[Strategy no. 4]{\label{fig:in}
    \begin{tikzpicture}[thick,scale=0.76, every node/.style={transform shape}]
    \centering
    \newcounter{k}
    \newcounter{l}
    \foreach \p/\t/\n [count=\j] in {
        41/Global equities/-15pt,
        41/Absolute Return Bonds/15pt,
        9/Euro Corporate bonds ($>$10 years)/15pt,
        9/Others\footnote\smallskip/10pt}
    {
        \setcounter{k}{\value{l}}
        \addtocounter{l}{\p}
        \slicepie{\thek/100*360}
        {\thel/100*360} 
        {\t}
        {\n}
    }

    \end{tikzpicture}
} 

\subfloat[Strategy no. 5]{\label{fig:in}

    \begin{tikzpicture}[thick,scale=0.76, every node/.style={transform shape}]
    \centering
    \newcounter{y}
    \newcounter{r}  
    \foreach \p/\t/\n [count=\j] in {       
        33/Absolute Return Bonds/0pt,
        52/Global equities/-3pt,            
        5/Cash/10pt,
        5/Others\footnote\smallskip/13pt,
        5/Euro Corporate bonds ($>$10 years)/15pt}
    {
        \setcounter{y}{\value{r}}
        \addtocounter{r}{\p}
        \slicepie{\they/100*360}
        {\ther/100*360} 
        {\t}
        {\n}
    }

    \end{tikzpicture}
}

    \subfloat[Strategy no. 6]{\label{fig:in}
    \begin{tikzpicture}[thick,scale=0.76, every node/.style={transform shape}] 
    \centering
    \newcounter{v}
    \newcounter{f} 
    \foreach \p/\t/\n [count=\j] in {
        37/Absolute Return Bonds/-15pt,
        6/Euro Corporate bonds ($>$10 years)/-10pt,
        3/Fixed Interest euro govt Bonds ($>$10years)/-15pt,
        47/Global equities/15pt,
        4/Others\footnotemark\smallskip/5pt,
        3/Eurozone Equities/10pt}
    {
        \setcounter{v}{\value{f}}
        \addtocounter{f}{\p}
        \slicepie{\thev/100*360}
        {\thef/100*360} 
        {\t}
        {\n}
    }

    \end{tikzpicture}
}


\caption{Alocation of the amount invested in each sub-asset class for the five strategies}
\label{fi2} 

\end{figure}
\end{center}

\footnotetext[1]{Euro Corporate Bonds ($>$10 years) (2\%), Eurozone Equities (2\%), Index-Linked Euro Government (1\%), Fixed Interest Euro Government Bonds ($>$10 years) (1\%), Cash (1\%), Others (1\%)}

\footnotetext[2]{Property (2\%), Eurozone Equities (2\%), Fixed Interest Euro Government Bonds ($>$5 years) (1\%), Cash (1\%), Emerging Market Debt (2\%)}

\footnotetext[3]{Eurozone Equities (2\%), Index-Linked Euro Government (2\%), Fixed Interest Euro Government Bonds ($>$5 years) (1\%), Fixed Interest Euro Government Bonds ($>$10 years) (2\%), Cash (2\%)}

\footnotetext[4]{Eurozone Equities (1\%), Index-Linked Euro Government (1\%), Fixed Interest Euro Government Bonds ($>$10 years) (1\%), Property (3\%)}

\footnotetext[5]{Index-Linked Euro Government (1\%), Fixed Interest Euro Government Bonds ($>$5 years) (2\%), Cash (1\%)}


\end{document}

您能帮我一下吗?谢谢。

答案1

我假设你会对齐所有切片

一个解决方案,不是很干净,但可以步行

我在你的命令 \slicepie 中添加了一个宽度大于你的图表的节点

\node [minimum width = 1.3 \ textwidth, draw] {};

  (删除绘图以获得最终渲染)

\centering然后,我移动了每个子图中的各种

\documentclass[oneside,12pt]{scrbook}

\usepackage{tikz}%esquema
\usepackage{subfig}

\begin{document}


\newcommand{\slicepie}[4]{
\pgfmathsetmacro\midangle{0.5*#1+0.5*#2}
% slice
\draw[thick,fill=black!10] (0,0) -- (#1:1) arc (#1:#2:1) -- cycle;
\ifnum\p<11
\def\plabel{#3 (\p\%)}%
\def\slabel{}%
\else
\def\plabel{#3}%
\def\slabel{\p\%}%
\fi
%outer label
\node  [label={[name=l \j]\midangle:\plabel}, yshift=#4] at (\midangle:1.15) {};
%inner label
\pgfmathsetmacro\innerpos{max(min((#2-#1-10)/110*(-0.3),0),-.5) +.8}
\pgfmathsetmacro\labelanchor{ ( \midangle < 90 ) ? "south west" : ( ( \midangle < 180 ) ? "east" : ( ( \midangle < 270 ) ? "north" : "west" ) ) }
\node (slabel \j) at (\midangle:\innerpos) {\slabel};
\draw (slabel \j) -- (l \j.\labelanchor) ;
\node[minimum width=1.3\textwidth,draw]{};
 }

 \begin{center}
 \begin{figure}[!ht]    
    \centering
\subfloat[Strategy no. 2]{\label{fig:in}
    \centering
    \begin{tikzpicture}[thick,scale=0.76, every node/.style={transform shape}]
    \newcounter{z}
    \newcounter{g}
    \foreach \p/\t/\n [count=\j] in {
        83/Global equities/-10pt,           
        9/Absolute Return Bonds/10pt,
        8/Others\footnote\smallskip/10pt}
    {
        \setcounter{z}{\value{g}}
        \addtocounter{g}{\p}
        \slicepie{\thez/100*360}
        {\theg/100*360} 
        {\t}
        {\n}
    }

    \end{tikzpicture}
}

\subfloat[Strategy no. 3]{\label{fig:in}
    \centering
    \begin{tikzpicture}[thick,scale=0.76, every node/.style={transform shape}]
    \newcounter{q}
    \newcounter{w}
    \foreach \p/\t/\n [count=\j] in {
        35/Absolute Return Bonds/0pt,
        7/Euro Corporate bonds ($>$10 years)/0pt,
        4/Eurozone Equities/0pt,
        4/Fixed Interest euro govt Bonds ($>$10years)/-5pt,
        43/Global equities/0pt,
        3/Index-Linked euro govts (AS)/10pt,
        8/Others\footnote\smallskip/15pt}
    {
        \setcounter{q}{\value{w}}
        \addtocounter{w}{\p}
        \slicepie{\theq/100*360}
        {\thew/100*360} 
        {\t}
        {\n}
    }

    \end{tikzpicture}
}

\subfloat[Strategy no. 4]{\label{fig:in}
    \centering
    \begin{tikzpicture}[thick,scale=0.76, every node/.style={transform shape}]

    \newcounter{k}
    \newcounter{l}
    \foreach \p/\t/\n [count=\j] in {
        41/Global equities/-15pt,
        41/Absolute Return Bonds/15pt,
        9/Euro Corporate bonds ($>$10 years)/15pt,
        9/Others\footnote\smallskip/10pt}
    {
        \setcounter{k}{\value{l}}
        \addtocounter{l}{\p}
        \slicepie{\thek/100*360}
        {\thel/100*360} 
        {\t}
        {\n}
    }

    \end{tikzpicture}
} 

\subfloat[Strategy no. 5]{\label{fig:in}
    \centering
    \begin{tikzpicture}[thick,scale=0.76, every node/.style={transform shape}]

    \newcounter{y}
    \newcounter{r}  
    \foreach \p/\t/\n [count=\j] in {       
        33/Absolute Return Bonds/0pt,
        52/Global equities/-3pt,            
        5/Cash/10pt,
        5/Others\footnote\smallskip/13pt,
        5/Euro Corporate bonds ($>$10 years)/15pt}
    {
        \setcounter{y}{\value{r}}
        \addtocounter{r}{\p}
        \slicepie{\they/100*360}
        {\ther/100*360} 
        {\t}
        {\n}
    }

    \end{tikzpicture}
}

    \subfloat[Strategy no. 6]{\label{fig:in}
    \centering
    \begin{tikzpicture}[thick,scale=0.76, every node/.style={transform shape}] 
    \centering
    \newcounter{v}
    \newcounter{f} 
    \foreach \p/\t/\n [count=\j] in {
        37/Absolute Return Bonds/-15pt,
        6/Euro Corporate bonds ($>$10 years)/-10pt,
        3/Fixed Interest euro govt Bonds ($>$10years)/-15pt,
        47/Global equities/15pt,
        4/Others\footnotemark\smallskip/5pt,
        3/Eurozone Equities/10pt}
    {
        \setcounter{v}{\value{f}}
        \addtocounter{f}{\p}
        \slicepie{\thev/100*360}
        {\thef/100*360} 
        {\t}
        {\n}
    }

    \end{tikzpicture}
}


\caption{Alocation of the amount invested in each sub-asset class for the five strategies}
\label{fi2} 

\end{figure}
\end{center}

\footnotetext[1]{Euro Corporate Bonds ($>$10 years) (2\%), Eurozone Equities (2\%), Index-Linked Euro Government (1\%), Fixed Interest Euro Government Bonds ($>$10 years) (1\%), Cash (1\%), Others (1\%)}

\footnotetext[2]{Property (2\%), Eurozone Equities (2\%), Fixed Interest Euro Government Bonds ($>$5 years) (1\%), Cash (1\%), Emerging Market Debt (2\%)}

\footnotetext[3]{Eurozone Equities (2\%), Index-Linked Euro Government (2\%), Fixed Interest Euro Government Bonds ($>$5 years) (1\%), Fixed Interest Euro Government Bonds ($>$10 years) (2\%), Cash (2\%)}

\footnotetext[4]{Eurozone Equities (1\%), Index-Linked Euro Government (1\%), Fixed Interest Euro Government Bonds ($>$10 years) (1\%), Property (3\%)}

\footnotetext[5]{Index-Linked Euro Government (1\%), Fixed Interest Euro Government Bonds ($>$5 years) (2\%), Cash (1\%)}


\end{document}

在此处输入图片描述

答案2

另一种解决方案是使用xcenter around将 TikZ 图片置于某个区域的中心。在那里.style定义了两个参数,但在这里它被调整为只使用一个。

主要思想与rpapa 的回答,但不是添加node足够宽来包含完整的图片,而是xcenter around自动修改图形的最终边界框。

\documentclass[oneside,12pt]{scrbook}

\usepackage{tikz}%esquema
\usetikzlibrary{calc}

\tikzset{%
    xcenter around/.style={execute at end picture={%
          \useasboundingbox let \p0 = (current bounding box.south west), 
                                \p1 = (current bounding box.north east),
                                \p2 = (#1)
          in ({min(\x2 - \x1,\x0)},\y0) rectangle 
             ({max(\x2 - \x0,\x1)},\y1);
}}}

\usepackage{subfig}

\begin{document}
\newcommand{\slicepie}[4]{%
    \pgfmathsetmacro\midangle{0.5*#1+0.5*#2}
    % slice
    \draw[thick,fill=black!10] (0,0) -- (#1:1) arc (#1:#2:1) -- cycle;
    \ifnum\p<11
        \def\plabel{#3 (\p\%)}%
        \def\slabel{}%
    \else
        \def\plabel{#3}%
        \def\slabel{\p\%}%
    \fi
    %outer label
    \node [label={[name=l \j]\midangle:\plabel}, yshift=#4] at 
          (\midangle:1.15) {};
    %inner label
    \pgfmathsetmacro\innerpos{max(min((#2-#1-10)/110*(-0.3),0),-.5) +.8}
    \pgfmathsetmacro\labelanchor{(\midangle<90)?"south west":
            ((\midangle<180)?"east":((\midangle<270)?"north":"west"))}
    \node (slabel \j) at (\midangle:\innerpos) {\slabel};
    \draw (slabel \j) -- (l \j.\labelanchor);
 }

 \begin{figure}[!ht]    
 \centering
 \subfloat[Strategy no. 2]{\label{fig:in}
 \begin{tikzpicture}[%
     thick,scale=0.76, every node/.style={transform shape}, 
     xcenter around={0,0}]
     \newcounter{z}
     \newcounter{g}
     \foreach \p/\t/\n [count=\j] in {
        83/Global equities/-10pt,           
        9/Absolute Return Bonds/10pt,
        8/Others\footnote\smallskip/10pt}
     {
        \setcounter{z}{\value{g}}
        \addtocounter{g}{\p}
        \slicepie{\thez/100*360}
        {\theg/100*360} 
        {\t}
        {\n}
     }
 \end{tikzpicture}
 }

 \subfloat[Strategy no. 3]{\label{fig:in}
    \begin{tikzpicture}[%
         thick,scale=0.76, every node/.style={transform shape}, 
         xcenter around={0,0}
    ]
    \newcounter{q}
    \newcounter{w}
    \foreach \p/\t/\n [count=\j] in {
        35/Absolute Return Bonds/0pt,
        7/Euro Corporate bonds ($>$10 years)/0pt,
        4/Eurozone Equities/0pt,
        4/Fixed Interest euro govt Bonds ($>$10years)/-5pt,
        43/Global equities/0pt,
        3/Index-Linked euro govts (AS)/10pt,
        8/Others\footnote\smallskip/15pt}
    {
        \setcounter{q}{\value{w}}
        \addtocounter{w}{\p}
        \slicepie{\theq/100*360}
        {\thew/100*360} 
        {\t}
        {\n}
    }
    \end{tikzpicture}
}

\subfloat[Strategy no. 4]{\label{fig:in}
    \begin{tikzpicture}[%
         thick, scale=0.76, every node/.style={transform shape}, 
         xcenter around={0,0}]
    \newcounter{k}
    \newcounter{l}
    \foreach \p/\t/\n [count=\j] in {
        41/Global equities/-15pt,
        41/Absolute Return Bonds/15pt,
        9/Euro Corporate bonds ($>$10 years)/15pt,
        9/Others\footnote\smallskip/10pt}
    {
        \setcounter{k}{\value{l}}
        \addtocounter{l}{\p}
        \slicepie{\thek/100*360}
        {\thel/100*360} 
        {\t}
        {\n}
    }

    \end{tikzpicture}
} 

\subfloat[Strategy no. 5]{\label{fig:in}
    \begin{tikzpicture}[%
        thick, scale=0.76, every node/.style={transform shape}, 
        xcenter around={0,0}]
    \newcounter{y}
    \newcounter{r}  
    \foreach \p/\t/\n [count=\j] in {       
        33/Absolute Return Bonds/0pt,
        52/Global equities/-3pt,            
        5/Cash/10pt,
        5/Others\footnote\smallskip/13pt,
        5/Euro Corporate bonds ($>$10 years)/15pt}
    {
        \setcounter{y}{\value{r}}
        \addtocounter{r}{\p}
        \slicepie{\they/100*360}
        {\ther/100*360} 
        {\t}
        {\n}
    }
    \end{tikzpicture}
}

    \subfloat[Strategy no. 6]{\label{fig:in}
    \begin{tikzpicture}[%
       thick, scale=0.76, every node/.style={transform shape}, 
       xcenter around={0,0}] 
    \newcounter{v}
    \newcounter{f} 
    \foreach \p/\t/\n [count=\j] in {
        37/Absolute Return Bonds/-15pt,
        6/Euro Corporate bonds ($>$10 years)/-10pt,
        3/Fixed Interest euro govt Bonds ($>$10years)/-15pt,
        47/Global equities/15pt,
        4/Others\footnotemark\smallskip/5pt,
        3/Eurozone Equities/10pt}
    {
        \setcounter{v}{\value{f}}
        \addtocounter{f}{\p}
        \slicepie{\thev/100*360}
        {\thef/100*360} 
        {\t}
        {\n}
    }
    \end{tikzpicture}
}


\caption{Alocation of the amount invested in each sub-asset class for the five strategies}
\label{fi2} 

\end{figure}

\footnotetext[1]{Euro Corporate Bonds ($>$10 years) (2\%), Eurozone Equities (2\%), Index-Linked Euro Government (1\%), Fixed Interest Euro Government Bonds ($>$10 years) (1\%), Cash (1\%), Others (1\%)}

\footnotetext[2]{Property (2\%), Eurozone Equities (2\%), Fixed Interest Euro Government Bonds ($>$5 years) (1\%), Cash (1\%), Emerging Market Debt (2\%)}

\footnotetext[3]{Eurozone Equities (2\%), Index-Linked Euro Government (2\%), Fixed Interest Euro Government Bonds ($>$5 years) (1\%), Fixed Interest Euro Government Bonds ($>$10 years) (2\%), Cash (2\%)}

\footnotetext[4]{Eurozone Equities (1\%), Index-Linked Euro Government (1\%), Fixed Interest Euro Government Bonds ($>$10 years) (1\%), Property (3\%)}

\footnotetext[5]{Index-Linked Euro Government (1\%), Fixed Interest Euro Government Bonds ($>$5 years) (2\%), Cash (1\%)}

\end{document}

在此处输入图片描述

相关内容