4

a) How can I resize the tikz picture to fit inside the tabular cell ?

b) How can I center the first row horizontally?

This is what I've until now:

\documentclass{article}
\usepackage[a4paper]{geometry}

\usepackage[utf8]{inputenc} % accents
\usepackage{verbatim} % \begin{comment}
\usepackage{tcolorbox}

\usepackage{amsfonts,amsmath}

\usepackage{tikz}

\newcommand{\mmRationalPie}[1]{
\def\angle{#1}
\def\radius{1}
\def\color{blue}

\begin{tikzpicture}[]
  \draw[fill=red,draw=red] (0,0) -- (0:\radius) arc (0:\angle:\radius) -- cycle;
  \draw (0,0) -- (\angle:\radius) arc (\angle:360:\radius) -- cycle;
\end{tikzpicture}
}

\begin{document}


\begin{tabular}[t]{ | p{1cm} | p{1cm} |}
$ \frac{1}{3} $ & $ \frac{2}{3} $ \\
\hline 
\mmRationalPie{120} & \mmRationalPie{240}
\end{tabular}


\end{document}

clarification: it is better if, in order to change the sizes, it is not necessary modify both, the tikz commands and the tabular characteristics.

5 Answers5

4

The problem is that you're setting the radius of the circle to be 1cm and the table column width to 1cm too. You should set the circle radius to be less than 0.5cm (less because there is inner spacing inside the tabular cells).

To center the first row you have to use centered column specifiers, if you wish to set the column width than you can make a new column specifier using the array package, like in this answer. If you are ok with leaving the width for LaTeX to determine, than the usual c can manage it. I've used TikZ pic's mechanism instead of your command, as that's more flexible (you can call options with it).

Finally, consider not using vertical rules, in this example I removed the vertical rules and used \toprule, \midrule and \bottomrule from booktabs, they offer better spacing between rule and text. The result:

enter image description here

EDIT: since the OP wants to have the circle radius to follow the column width I used the definition of \pieradius as a function of \linewidth. But the radius can also be set locally through the key pie radius=<dim>

MWE:

\documentclass{article}
\usepackage{tikz,array,booktabs,amsmath}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash}m{#1}}

\tikzset{
pie radius/.store in=\pieradius,
pie radius=0.45*\linewidth,
pics/rational pie/.style={code={\filldraw[red] (0,0) -- (0:.45*\pieradius) arc (0:#1:.45*\pieradius) -- cycle;%
      \draw (0,0) -- (#1:.45*\pieradius) arc (#1:360:.45*\pieradius) -- cycle;}}
}

\begin{document}
  \begin{tabular}[t]{C{1cm} C{2cm}}
  \toprule
  $ \dfrac{1}{3} $ & $ \dfrac{2}{3} $ \\
  \midrule
  \tikz\pic[pie radius=0.2cm]{rational pie=120}; & \tikz\pic{rational pie=240}; \\
  \tikz\pic{rational pie=120}; & \tikz\pic{rational pie=240}; \\
  \bottomrule
  \end{tabular}
\end{document}
3

To resize the drawing without changing the original definition, use the resize command of the form \resizebox{width}{height}{object} choosing the required width/height as you wish.

\documentclass{article}
\usepackage[a4paper]{geometry}
\usepackage[utf8]{inputenc} % accents
\usepackage{verbatim} % \begin{comment}
\usepackage{tcolorbox}
\usepackage{amsfonts,amsmath}
\usepackage{tikz,array,booktabs}

\newcommand{\mmRationalPie}[1]{
\def\angle{#1}
\def\radius{1}
\def\color{blue}
\begin{tikzpicture}[]
  \draw[fill=red,draw=red] (0,0) -- (0:\radius) arc (0:\angle:\radius) -- cycle;
  \draw (0,0) -- (\angle:\radius) arc (\angle:360:\radius) -- cycle;
\end{tikzpicture}
}

\begin{document}    
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}

\begin{tabular}[t]{ C{1cm} C{1cm} }
\toprule
$ \frac{1}{3} $ & $ \frac{2}{3} $ \\ \midrule 
\resizebox{1cm}{!}{\mmRationalPie{120}} & \resizebox{1cm}{!}{\mmRationalPie{240}}\\
\bottomrule
\end{tabular}

\end{document}

enter image description here

AboAmmar
  • 46,352
  • 4
  • 58
  • 127
2

Use the scale option of tikzpicture. Furthermore, avoid empty lines in the definition of the command, as well as spurious spaces because of line ends. I'd center the fraction and the diagram, either by changing the column type to c or by using multicolumn to change the type individually for single entries; in the code below I illustrate both variants.

enter image description here

\documentclass{article}
\usepackage{tikz}

\newcommand{\mmRationalPie}[1]{%
\def\angle{#1}%
\def\radius{1}%
\def\color{blue}%
\begin{tikzpicture}[scale=0.5] % <<<<<<<<<<<<<<<<<<<<<<<<<<<
  \draw[fill=red,draw=red] (0,0) -- (0:\radius) arc (0:\angle:\radius) -- cycle;
  \draw (0,0) -- (\angle:\radius) arc (\angle:360:\radius) -- cycle;
\end{tikzpicture}%
}

\begin{document}

\begin{tabular}[t]{ | c | p{1cm} |}
$ \frac{1}{3} $ & \multicolumn{1}{c|}{$\frac{2}{3}$} \\
\hline 
\mmRationalPie{120} & \multicolumn{1}{c|}{\mmRationalPie{240}}
\end{tabular}

\end{document}
gernot
  • 49,614
  • Thanks for the answer. My objective is set sizes only in the tabular definition, draw should adapt to it. In this way I can use the draw newcommand in other tables with different column sizes. – pasaba por aqui Nov 27 '16 at 19:53
  • @pasabaporaqui I've added another solution which scales automatically. There is a second issue involved: in all proposed solutions, the 120-pie has extra space to its left, which shifts the circle to the right and makes it also turn out smaller. This is maybe worth an extra question. – gernot Nov 27 '16 at 20:36
2

Here is a solution based on this answer to "How to scale a tikzpicture to \textwidth", which uses an idea due to Ulrike Fischer. It is recommended not to scale a tikz-picture externally, therefore the tikz-picture is typeset twice, once unscaled and then with an appropriate scaling factor computed on the basis of the unscaled width.

I did not remove the vertical rules to show that for some reason the tikz-picture in the left column has some extra space (originating already from the picture) such that the circle turns out smaller than the right one. But this is another question ...

\documentclass{article}
\usepackage{tikz}
\usepackage{environ}
\makeatletter
\newsavebox{\measure@tikzpicture}
\NewEnviron{scaletikzpicturetowidth}[1]%
  {\def\tikz@width{#1}%
   \def\tikzscale{1}%
   \begin{lrbox}{\measure@tikzpicture}%
     \BODY
   \end{lrbox}%
    \pgfmathparse{#1/\wd\measure@tikzpicture}%
    \edef\tikzscale{\pgfmathresult}%
    \BODY
  }
\makeatother

\newcommand{\mmRationalPie}[1]%
  {\begin{scaletikzpicturetowidth}{\linewidth}%
     \def\pieangle{#1}%
     \def\pieradius{1}%
     \begin{tikzpicture}[scale=\tikzscale]
       \draw[fill=red,draw=red] (0,0) -- (0:\pieradius) arc (0:\pieangle:\pieradius) -- cycle;
       \draw (0,0) -- (\pieangle:\pieradius) arc (\pieangle:360:\pieradius) -- cycle;
     \end{tikzpicture}%
   \end{scaletikzpicturetowidth}%
  }

\begin{document}

\begin{tabular}[t]{| p{1cm} | p{1cm} |}
\multicolumn{1}{|c|}{$\frac{1}{3}$} & \multicolumn{1}{c|}{$\frac{2}{3}$} \\
\hline 
\mmRationalPie{120} & \mmRationalPie{240}
\end{tabular}

\end{document}

enter image description here

gernot
  • 49,614
  • why is not recommended use "resizebox" ? Another answer used it. – pasaba por aqui Nov 27 '16 at 21:20
  • @pasabaporaqui It scales everything including line widths and text. It leads to inconsistencies in the widths of lines and the size of text between pictures in the same document. It should, therefore, be an option of last resort. – cfr Nov 28 '16 at 03:30
  • Have you considered using the tikzscale package? \def\tikzscale invites significant opportunities for confusion, it seems to me. – cfr Nov 28 '16 at 03:34
  • Please change the \defs!! \def\color{blue}??!! Ouch. – cfr Nov 28 '16 at 04:23
2

I wanted to do this with tikzscale and couldn't, so this is semi-done.

Very important: never write things like \def\color{blue} unless you really, really want things to break!

I wouldn't do this this way at all, but since you seem determined to do so, at least avoid the most obvious sources of trouble.

\documentclass{article}
\usepackage[a4paper]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{array}
\usepackage{tikz}

\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}

\tikzset{%
  rat pie/.search also={/tikz/},
  rat pie/.cd,
  angle/.store in=\ratpieangle,
  radius/.store in=\ratpieradius,
  colour/.store in=\ratpiecolour,
  angle=60,
  radius=1,
  colour=blue,
}
\newcommand{\mmRationalPie}[1]{%
  \tikzset{%
    rat pie/angle=#1,
    rat pie/radius=.45\linewidth,
  }%
  \begin{tikzpicture}[baseline=(o)]
    \draw [fill=\ratpiecolour, draw=\ratpiecolour] (0,0) coordinate (o) -- (0:\ratpieradius ) arc (0:\ratpieangle:\ratpieradius) -- cycle;
    \draw (0,0) -- (\ratpieangle:\ratpieradius) arc (\ratpieangle:360:\ratpieradius) -- cycle;
  \end{tikzpicture}%
}

\begin{document}

\begin{tabular}[t]{ | C{1cm} | C{1cm} |}
  $ \frac{1}{3} $ & $ \frac{2}{3} $ \\
  \hline
  \mmRationalPie{120} & \mmRationalPie{240}\\
  \mmRationalPie{240} & \mmRationalPie{30}\\
\end{tabular}

\end{document}

avoid worst trouble?

cfr
  • 198,882