2

I have three figures which I want to group together into one figure with subcaptions, because they have the same source: enter image description here

I am using \subfloat for my other figures (got it from https://tex.stackexchange.com/a/246285/164280), which makes it look like this: enter image description here

Whenever I try to use \subfloat for my custom wheelchart (from https://tex.stackexchange.com/a/436536/164280 and https://tex.stackexchange.com/a/18105/164280), TeXstudio says I'm missing brackets or have too many brackets. Either way it crashes. My code for the current output is:

\documentclass[11pt, twoside, a4paper]{report}
\usepackage[inner = 30mm, outer = 20mm,  top = 30mm, bottom = 20mm, headheight = 13.6pt]{geometry}
\usepackage{emptypage}
\usepackage[toc,page]{appendix}
\usepackage{tikz}
\usepackage[pdfpagelayout=TwoPageRight]{hyperref}
\usepackage{multicol}
\usepackage{apacite}
\usepackage{textcomp}
\usepackage{chngcntr}
\usepackage{adjustbox}
\hypersetup{colorlinks=true, linktoc=all, allcolors=green!30!black,}
\usepackage{booktabs, siunitx, caption}
\newcommand{\source}[1]{\vspace{-8pt} \caption*{ Source: {#1}} }
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{slashbox}
\usepackage[caption=false]{subfig}
\usepackage{array}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11,
/pgfplots/ybar legend/.style={
    /pgfplots/legend image code/.code={%
        \draw[##1,/tikz/.cd,yshift=-0.25em]
        (0cm,0cm) rectangle (3pt,0.8em);},},}
\usetikzlibrary{arrows}
\begin{filecontents}{installations.csv}
Name,   Quantity
"Japan", 66
"China", 12
"Korea", 8
"Other", 23
\end{filecontents}

\begin{filecontents}{installedcapacity.csv}
Name,   Quantity
"Japan", 95.516
"China", 394.258
"Korea", 16.901
"Other", 14.589
\end{filecontents}

\begin{document}
%Wheelchart specifications
\pgfplotstableread[ignore chars=",col sep=comma]{installations.csv}\datainstallations
\pgfplotstableread[ignore chars=",col sep=comma]{installedcapacity.csv}\datacapacity
\pgfplotstableread{
clr
blue!70!black
red!70!black
black!70!white
white!70!black
}\MyColors

% Adjusts the size of the wheel:
\def\innerradius{1.5cm}
\def\outerradius{2cm}

\newcommand\LabelName{}
\newcommand\LabelValue{}

% The main macro
\newcommand{\wheelchart}[2]{ % <--- (from 1 to 2)
% Calculate total
\pgfmathsetmacro{\totalnum}{0}
% get number of rows in table
\pgfplotstablegetrowsof{#1}
% minus 1 because indexing starts as zero
\pgfmathsetmacro{\RowsInTable}{\pgfplotsretval-1}
\foreach \i in {0,...,\RowsInTable} {
    \pgfplotstablegetelem{\i}{Quantity}\of{#1}
    \pgfmathparse{\pgfplotsretval+\totalnum}
    \global\let\totalnum=\pgfmathresult
}
\begin{center}
    \begin{tikzpicture}
    % The text in the center of the wheel
    \node[align=center,text width=2*\innerradius]            {\pgfmathprintnumber{\totalnum}~{#2}}; % <--- (name is now defined by second     option of the command \wheelchart
    % Calculate the thickness and the middle line of the wheel
    \pgfmathsetmacro{\wheelwidth}{\outerradius-\innerradius}
    \pgfmathsetmacro{\midradius}{(\outerradius+\innerradius)/2}

    % Rotate so we start from the top
    \begin{scope}[rotate=90]
    % Loop through each value set. \cumnum keeps track of where we are in the wheel
    \pgfmathsetmacro{\cumnum}{0}
    \foreach \i in {0,...,\RowsInTable} {
        % get values from table
        \pgfplotstablegetelem{\i}{Name}\of{#1}\global\let\LabelName=\pgfplotsretval
        \pgfplotstablegetelem{\i}{Quantity}\of{#1}\renewcommand\LabelValue{\pgfplotsretval}

        \pgfmathsetmacro{\newcumnum}{\cumnum + \LabelValue/\totalnum*360}

        % Calculate the percent value
        \pgfmathsetmacro{\percentage}{\LabelValue}
        % Calculate the mid angle of the colour segments to place the labels
        \pgfmathsetmacro{\midangle}{-(\cumnum+\newcumnum)/2}

        % This is necessary for the labels to align nicely
        \pgfmathparse{
            (-\midangle<180?"west":"east")
        } \edef\textanchor{\pgfmathresult}
        \pgfmathsetmacro\labelshiftdir{ifthenelse(\RowsInTable==0,-1,1)*(1-2*(-\midangle>180))}

        % Draw the color segments. Somehow, the \midrow units got lost, so we add 'pt' at the end. Not nice...
        \pgfplotstablegetelem{\i}{clr}\of{\MyColors}
        \fill[color=\pgfplotsretval] (-\cumnum:\outerradius) arc (-\cumnum:-(\newcumnum):\outerradius) --
        (-\newcumnum:\innerradius) arc (-\newcumnum:-(\cumnum):\innerradius) -- cycle;

        % Draw the data labels
        \pgfmathsetmacro\labelmag{
            1 + .9*(abs(1/sin(\midangle)))^1 - .03*(abs(1/sin(\midangle)))^2}
        \draw  [*-,thin] node [append after command={(\midangle:\midradius pt) --
            (\midangle:\outerradius + \labelmag ex) -- (\tikzlastnode)}] at
        (\midangle:\outerradius + \labelmag ex)
        [xshift=\labelshiftdir*0.5cm,inner sep=0pt, outer sep=0pt, ,anchor=\textanchor]{\LabelName: \pgfmathprintnumber{\percentage}};

        % Set the old cumulated angle to the new value
        \global\let\cumnum=\newcumnum
    }
    \end{scope}
    %      \draw[gray] (0,0) circle (\outerradius) circle (\innerradius);
    \end{tikzpicture}
\end{center}
}

\begin{figure}[!htb]
\centering
\begin{tikzpicture}
\begin{axis}[
width=0.9\linewidth,
height=0.4\linewidth,
ybar,bar width=25pt, enlarge x limits=0.15, ymin=0, 
legend style={at={(0.5,1.15)},anchor=north,legend columns=2},
ylabel={Installed capacity (MW\textsubscript{p})},
xticklabels={$<2014$, $2014$, $2015$, $2016$, $2017$}, % set ticklabels explicitly
xtick=data, nodes near coords,  axis lines*=left, ymajorgrids
]
\addplot[red!50!black, fill=red!70!black] coordinates {(2013, 1.7) (2014, 3.0) (2015, 49.5) (2016, 70.7) (2017, 396.5)};
\addplot[blue!50!black, fill=blue!70!black] coordinates {(2013, 1.7) (2014, 4.7) (2015, 54.2) (2016, 124.9)(2017, 521.4)};
\legend{Annual installed capacity, Cumulative installed capacity}
\end{axis}
\end{tikzpicture}
\caption{Global installed capacity of floating photovoltaics}
\label{fig:capacity over time}
\end{figure}

What I'm trying to do is:

\begin{figure}[h]
\centering
\subfloat[Global installed capacity of floating photovoltaics]{%
\begin{tikzpicture}
\begin{axis}[
width=0.9\linewidth,
height=0.4\linewidth,
ybar,bar width=25pt, enlarge x limits=0.15, ymin=0, 
legend style={at={(0.5,1.15)},anchor=north,legend columns=2},
ylabel={Installed capacity (MW\textsubscript{p})},
xticklabels={$<2014$, $2014$, $2015$, $2016$, $2017$}, % set ticklabels explicitly
xtick=data, nodes near coords,  axis lines*=left, ymajorgrids
]
\addplot[red!50!black, fill=red!70!black] coordinates {(2013, 1.7) (2014, 3.0) (2015, 49.5) (2016, 70.7) (2017, 396.5)};
\addplot[blue!50!black, fill=blue!70!black] coordinates {(2013, 1.7) (2014, 4.7) (2015, 54.2) (2016, 124.9)(2017, 521.4)};
\legend{Annual installed capacity, Cumulative installed capacity}
\end{axis}
\end{tikzpicture}
}
\subfloat[Installations per country]{%
\wheelchart{\datainstallations}{Installations}
}
\subfloat[Installed capacity per country]{%
\wheelchart{\datacapacity}{MW\textsubscript{p}}
}   
\caption{FPV installations and capacity in late 2017}
\label{fig:capacity and installations per country}
\source{Adapted from \shortcite{DeJong2018}}    
\end{figure}

I hope you guys get what I'm trying to do. I want figure 1.1,1.2 and 1.3 to become 1.1 (a), 1.1 (b), 1.1 (c) and add the source at the bottom

Hans
  • 491
  • Probably unrelated, but your \source command misses its closing }. Instead of the subfig command from the subfig package, you could try the subfigure environment from the subcaption package. (As a side note: both packages are incompatible with each other.) – leandriis Jul 08 '18 at 14:59
  • @leandriis The } was a typo. Thanks for spotting it, but does not change anything. I'd rather not switch to another environment, because I have all my figures in the subfig style now – Hans Jul 08 '18 at 15:36
  • If you prefer to stick to the subfig package, you could try to enclose both \wheelchart commands in \begin{minipage}[b]{0.5\textwidth} commands, as already done by Zarko in their anwer to your previous question. – leandriis Jul 08 '18 at 16:37
  • I believe that these are expansion issues. Not even putting the wheel charts in saveboxes works. As a temporary workaround you may consider producing pdf pictures of the wheel charts using standalone and then including those in your document. –  Jul 08 '18 at 17:29

1 Answers1

2

You can't have a center environment inside a \subfloat it seems, and the definition of \wheelchart includes one. Comment out/remove \begin{center} and \end{center} in the \wheelchart macro.

I also made some other changes to \wheelchart, see comments in code. Most notably I added a bunch of % at ends of lines, to avoid spurious spaces. I also added an optional argument that is passed to the tikzpicture, which lets you for example scale the chart.

Edit: There are probably several ways of fixing the vertical alignment. One way would be to add something like \path (-90:1.25*\outerradius); to the tikzpicture. That will extend the bounding box downwards, and if the bounding box of both wheels extend the same distance below the wheel, the alignment is correct. To avoid making that the default (which will cause additional whitespace even in cases where it's not needed), you could define a new style with

\tikzset{
  raisewheel/.style={
    execute at end picture={
      \path (-90:#1*\outerradius);
    }
  },
  raisewheel/.default=1.3
}

and use e.g.

\wheelchart[scale=0.8,font=\footnotesize,raisewheel]{\datainstallations}{Installations}

Not very elegant perhaps, but it does work.

enter image description here

\documentclass[11pt, twoside, a4paper]{report}
\usepackage[inner = 30mm, outer = 20mm,  top = 30mm, bottom = 20mm, headheight = 13.6pt]{geometry}
\usepackage{emptypage}
\usepackage[toc,page]{appendix}
\usepackage{tikz}
\usepackage[pdfpagelayout=TwoPageRight]{hyperref}
\usepackage{multicol}
\usepackage{apacite}
\usepackage{textcomp}
\usepackage{chngcntr}
\usepackage{adjustbox}
\hypersetup{colorlinks=true, linktoc=all, allcolors=green!30!black,}
\usepackage{booktabs, siunitx, caption}
\newcommand{\source}[1]{\vspace{-8pt} \caption*{ Source: {#1}} }
\usepackage{amsmath}
\usepackage{graphicx}
%\usepackage{slashbox}
\usepackage[caption=false]{subfig}
\usepackage{array}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11,
/pgfplots/ybar legend/.style={
    /pgfplots/legend image code/.code={%
        \draw[##1,/tikz/.cd,yshift=-0.25em]
        (0cm,0cm) rectangle (3pt,0.8em);},},}
\usetikzlibrary{arrows,backgrounds}


\tikzset{
  raisewheel/.style={
    execute at end picture={
      \path (-90:#1*\outerradius);
    }
  },
  raisewheel/.default=1.3
}

\begin{filecontents}{installations.csv}
Name,   Quantity
"Japan", 66
"China", 12
"Korea", 8
"Other", 23
\end{filecontents}

\begin{filecontents}{installedcapacity.csv}
Name,   Quantity
"Japan", 95.516
"China", 394.258
"Korea", 16.901
"Other", 14.589
\end{filecontents}

\begin{document}
%Wheelchart specifications
\pgfplotstableread[ignore chars=",col sep=comma]{installations.csv}\datainstallations
\pgfplotstableread[ignore chars=",col sep=comma]{installedcapacity.csv}\datacapacity
\pgfplotstableread{
clr
blue!70!black
red!70!black
black!70!white
white!70!black
}\MyColors

% Adjusts the size of the wheel:
\def\innerradius{1.6cm}
\def\outerradius{2cm}

\newcommand\LabelName{}
\newcommand\LabelValue{}

% The main macro
\newcommand{\wheelchart}[3][]{ % <--- (from 2 to 3, first argument optional)
% Calculate total
\pgfmathsetmacro{\totalnum}{0}% <-- added %
% get number of rows in table
\pgfplotstablegetrowsof{#2}% <-- added %
% minus 1 because indexing starts as zero
\pgfmathsetmacro{\RowsInTable}{\pgfplotsretval-1}% <-- added %
\foreach \i in {0,...,\RowsInTable} {% <-- added %
    \pgfplotstablegetelem{\i}{Quantity}\of{#2}% <-- added %
    \pgfmathparse{\pgfplotsretval+\totalnum}% <-- added %
    \global\let\totalnum=\pgfmathresult% <-- added %
}
%\begin{center}  % <---- commented
    \begin{tikzpicture}[#1] % <--- added [#1]
    % The text in the center of the wheel
    \node[align=center,text width=2*\innerradius]            {\pgfmathprintnumber{\totalnum}~{#3}}; % <--- (name is now defined by second     option of the command \wheelchart
    % Calculate the thickness and the middle line of the wheel
    \pgfmathsetmacro{\wheelwidth}{\outerradius-\innerradius}
    \pgfmathsetmacro{\midradius}{(\outerradius+\innerradius)/2}

    % Rotate so we start from the top
    \begin{scope}[rotate=90]
    % Loop through each value set. \cumnum keeps track of where we are in the wheel
    \pgfmathsetmacro{\cumnum}{0}
    \foreach \i in {0,...,\RowsInTable} {
        % get values from table
        \pgfplotstablegetelem{\i}{Name}\of{#2}\global\let\LabelName=\pgfplotsretval
        \pgfplotstablegetelem{\i}{Quantity}\of{#2}\renewcommand\LabelValue{\pgfplotsretval}

        \pgfmathsetmacro{\newcumnum}{\cumnum + \LabelValue/\totalnum*360}

        % Calculate the percent value
        \pgfmathsetmacro{\percentage}{\LabelValue}
        % Calculate the mid angle of the colour segments to place the labels
        \pgfmathsetmacro{\midangle}{-(\cumnum+\newcumnum)/2}

        % This is necessary for the labels to align nicely
        \pgfmathparse{
            (-\midangle<180?"west":"east")
        } \edef\textanchor{\pgfmathresult}
        \pgfmathsetmacro\labelshiftdir{ifthenelse(\RowsInTable==0,-1,1)*(1-2*(-\midangle>180))}

        % Draw the color segments. Somehow, the \midrow units got lost, so we add 'pt' at the end. Not nice...
        \pgfplotstablegetelem{\i}{clr}\of{\MyColors}
        \fill[color=\pgfplotsretval] (-\cumnum:\outerradius) arc (-\cumnum:-(\newcumnum):\outerradius) --
        (-\newcumnum:\innerradius) arc (-\newcumnum:-(\cumnum):\innerradius) -- cycle;

        % Draw the data labels
        \pgfmathsetmacro\labelmag{
            1 + .9*(abs(1/sin(\midangle)))^1 - .03*(abs(1/sin(\midangle)))^2}
        \draw  [*-,thin] node [append after command={(\midangle:\midradius pt) --
            (\midangle:\outerradius + \labelmag ex) -- (\tikzlastnode)}] at
        (\midangle:\outerradius + \labelmag ex)
        [xshift=\labelshiftdir*0.5cm,inner sep=0pt, outer sep=0pt, ,anchor=\textanchor]{\LabelName: \pgfmathprintnumber{\percentage}};

        % Set the old cumulated angle to the new value
        \global\let\cumnum=\newcumnum
    }
    \end{scope}
    %      \draw[gray] (0,0) circle (\outerradius) circle (\innerradius);
    \end{tikzpicture}
%\end{center} % <---- commented
}


\begin{figure}[h]
\centering
\subfloat[Global installed capacity of floating photovoltaics]{%
\begin{tikzpicture}
\begin{axis}[
width=0.9\linewidth,
height=0.4\linewidth,
ybar,bar width=25pt, enlarge x limits=0.15, ymin=0, 
legend style={at={(0.5,1.15)},anchor=north,legend columns=2},
ylabel={Installed capacity (MW\textsubscript{p})},
xticklabels={$<2014$, $2014$, $2015$, $2016$, $2017$}, % set ticklabels explicitly
xtick=data, nodes near coords,  axis lines*=left, ymajorgrids
]
\addplot[red!50!black, fill=red!70!black] coordinates {(2013, 1.7) (2014, 3.0) (2015, 49.5) (2016, 70.7) (2017, 396.5)};
\addplot[blue!50!black, fill=blue!70!black] coordinates {(2013, 1.7) (2014, 4.7) (2015, 54.2) (2016, 124.9)(2017, 521.4)};
\legend{Annual installed capacity, Cumulative installed capacity}
\end{axis}
\end{tikzpicture}
}

\subfloat[Installations per country]{%
\wheelchart[scale=0.8,font=\footnotesize,raisewheel]{\datainstallations}{Installations}
}%
\subfloat[Installed capacity per country]{%
    \wheelchart[scale=0.8,font=\footnotesize,raisewheel]{\datacapacity}{MW\textsubscript{p}}
}
\caption{FPV installations and capacity in late 2017}
\label{fig:capacity and installations per country}
\source{Adapted from \shortcite{DeJong2018}}    
\end{figure}

\end{document}
Torbjørn T.
  • 206,688
  • T. Thanks a lot for the help. I have one issue which I had before: the wheelcharts are not vertically aligned. The text label of fig (c) causes the wheel to be higher than the wheel of (b). I have no idea how to fix that – Hans Jul 11 '18 at 13:59
  • @Hans See updated answer. – Torbjørn T. Jul 11 '18 at 14:46