3

I have this code to my pie graphs and I am trying to have the labels of the subfloats and each pie centered.

Here is my code:

\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}

Could u give me some help? thanks.

2 Answers2

3

I presume you would align all slicepie

a solution, not very clean but walking

I added in your command \ slicepie, a node whose width is larger than your diagrams

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

  (Remove the draw for the final rendering)

Then, I moved the various \centering in each sub figure

\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}

enter image description here

rpapa
  • 12,350
  • Since the picture is centered in a node that is wider than the text, the picture is not centered in the text block. An alternative could be to adjust the bounding box by adding trim left=-0.5\linewidth,trim right=0.5\linewidth as option to tikzpicture. However, then the text in the picture is too long and will run out in the left margin. So I am not sure it is an improvement for this case. – StefanH Oct 11 '16 at 13:28
0

An alternative solution could be using xcenter around style defined in Centering a TikZ picture around an area. There the .style was defined with two parameters, but here it has been adapted to use only one.

The main idea is similar to what's shown in rpapa's answer, but instead of adding a node wide enough to contain the complete picture, xcenter around automaticaly modifies the final bounding box of the figure.

\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}

enter image description here

Ignasi
  • 136,588