4

I have three pie charts for which I only want to create one legend. For this purpose, only the third diagram contains a legend. But my third diagram contains only one value. To create the legend, I have zeroed all the values that appear in the three pie charts in the third chart. The result then looks similar to the first figure.

\documentclass{article}

\usepackage{pgf-pie}

\begin{document}

\begin{tikzpicture}
\pie[pos={0,0},radius=1.5,sum=auto,text=]{22/Apple pie ,5/Cheesecake}
\pie[pos={4,0},radius=1.5,sum=auto,text=]{17/Apple pie,9/Cheesecake,1/Raspberry pie}
\pie[pos={8,0},radius=1.5,sum=auto,text=legend] {27/Apple pie,0/Cheesecake,0/Raspberry pie}
\end{tikzpicture}

\end{document}

Result: Result

Is it possible to display only values greater than 0 in the pie chart? I would like to get a result similar to the one in the second figure. I would prefer not to change the order of the diagrams.

Desired is: Desired

1 Answer 1

6

You can use the before number hook for that. This is what gets executed before the number.

\documentclass{article}

\usepackage{pgf-pie}

\begin{document}

\begin{tikzpicture}
\def\printonlypositive#1{\ifnum#1>0
#1
\fi}
\pie[pos={0,0},radius=1.5,sum=auto,text=]{22/Apple pie ,5/Cheesecake}
\pie[pos={4,0},radius=1.5,sum=auto,text=]{17/Apple pie,9/Cheesecake,1/Raspberry pie}
\pie[pos={8,0},radius=1.5,sum=auto,text=legend,
before number=\printonlypositive] {27/Apple pie,0/Cheesecake,0/Raspberry pie}
\end{tikzpicture}
\end{document}

enter image description here

A somewhat more robust version thereof that also works with noninteger numbers is

\documentclass{article}

\usepackage{pgf-pie}

\begin{document}

\begin{tikzpicture}
\def\printonlypositive#1{\ifdim#1pt>0pt
#1
\fi}
\pie[pos={0,0},radius=1.5,sum=auto,text=]{22/Apple pie ,5/Cheesecake}
\pie[pos={4,0},radius=1.5,sum=auto,text=]{17/Apple pie,9/Cheesecake,1/Raspberry pie}
\pie[pos={8,0},radius=1.5,sum=auto,text=legend,
before number=\printonlypositive] {27/Apple pie,0/Cheesecake,0/Raspberry pie}
\end{tikzpicture}
\end{document}
4
  • A very elegant way. I like that. Thank you very much! Nov 3, 2019 at 20:27
  • I used \def\printonlypositive#1{\pgfmathparse{#1>0 ? "#1" : ""}\pgfmathresult} here: tex.stackexchange.com/a/525838/46023
    – cis
    Jan 27, 2020 at 17:51
  • @cis This also works, I think, but internally the > comparison is an \ifdim statement, so it is not that different.
    – user194703
    Jan 27, 2020 at 18:06
  • Would it be possible to hide the entry from the legend as well? Jul 6, 2022 at 6:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .