2

As a follow-up question from this post I am trying to find out how to arrange multiple tikspictures in a table, where my diagrams are defined by my own macros.

Problem: Showing the diagrams separately works, but they are not loaded in a table. Furthermore I want the table to cover the textwidth of my document.

My question is similar to this one with the difference that I'm trying to call a command which I defined in the preamble.

    \documentclass[a4paper,11pt,twoside]{book}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\usepackage{tabularx}

\newcommand{\clocks}[4]{
\begin{figure}[htb]
% \resizebox{#5 \textwidth}{!}{
\begin{tikzpicture}
\begin{axis}[
  ybar,
%  enlargelimits=0.25,
%  legend style={at={(0.5,-0.15)},
%  anchor=north,legend columns=-1},
  ylabel={CPU clocks},
  symbolic x coords={one, two, three, four},
  xtick=data,
  nodes near coords,
  nodes near coords align={vertical},
]
\addplot coordinates {
(one, #1)
(two, #2)
(three, #3)
(four, #4)};
%\legend{one, two, three, four}
\end{axis}
\end{tikzpicture}
%}
\end{figure}
}


\begin{document}

\clocks{20}{50}{30}{70}

\begin{tabularx}{\textwidth}{|X|X|}
\hline
A & B \\
\hline
C & D \\
\hline
\end{tabularx}

%this table should be scaled to pagewidth!
\begin{tabularx}{\textwidth}{|X|X|}
\hline
\clocks{20}{50}{30}{70} & \clocks{20}{50}{30}{70} \\
\hline
\clocks{20}{50}{30}{70} & \clocks{20}{50}{30}{70} \\
\hline
\end{tabularx}

\end{document}
  • Why don't you use minipage-s instead of a tabularx?! \begin{minipage}{0.5\textwidth}...\end{minipage} – masu Feb 4 '14 at 13:14
  • Because the errors are the same ("Not in outer par mode", "Undefined control sequence") as when using tabluarx. At least % first column \begin{minipage}[t]{0.5\textwidth} \clocks{20}{50}{30}{70} \\ \clocks{20}{50}{30}{70} \end{minipage} %second column \begin{minipage}[t]{0.5\textwidth} \clocks{20}{50}{30}{70} \\ \clocks{20}{50}{30}{70} \end{minipage} doesnt work – bogus Feb 4 '14 at 13:33
  • Why are you using figure? Is not enough to use only tikzpicture? Also, since you picture has a fixed width you will have problems to fit two of them in a row. – Sigur Feb 4 '14 at 13:41
1

You cannot use a floating object (figure) inside a non-floating structure such as tabularx; remove the figure environment from the definition of \clock, and use an optional argument to control the width (I set the default value to \linewidth, but use whichever value you like better):

\documentclass[a4paper,11pt,twoside]{book}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{tabularx}

\pgfplotsset{compat=newest}

\newcommand{\clocks}[5][\linewidth]{%
% \resizebox{#5 \textwidth}{!}{
\begin{tikzpicture}
\begin{axis}[
  width=#1,
  ybar,
%  enlargelimits=0.25,
%  legend style={at={(0.5,-0.15)},
%  anchor=north,legend columns=-1},
  ylabel={CPU clocks},
  symbolic x coords={one, two, three, four},
  xtick=data,
  nodes near coords,
  nodes near coords align={vertical},
]
\addplot coordinates {
(one, #2)
(two, #3)
(three, #4)
(four, #5)};
%\legend{one, two, three, four}
\end{axis}
\end{tikzpicture}%
%}
}

\begin{document}

\noindent\begin{tabularx}{\textwidth}{|X|X|}
\hline
\clocks{20}{50}{30}{70} & \clocks{20}{50}{30}{70} \\
\hline
\clocks{20}{50}{30}{70} & \clocks{20}{50}{30}{70} \\
\hline
\end{tabularx}

\end{document}

enter image description here

Suppressing the lines from the table, and using enlargelimits, the result has a cleaner aspect:

\documentclass[a4paper,11pt,twoside]{book}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{tabularx}

\pgfplotsset{compat=newest}

\newcommand{\clocks}[5][\linewidth]{%
\begin{tikzpicture}
\begin{axis}[
  width=#1,
  ybar,
  enlargelimits=0.25,
%  legend style={at={(0.5,-0.15)},
%  anchor=north,legend columns=-1},
  ylabel={CPU clocks},
  symbolic x coords={one, two, three, four},
  xtick=data,
  nodes near coords,
  nodes near coords align={vertical},
]
\addplot coordinates {
(one, #2)
(two, #3)
(three, #4)
(four, #5)};
%\legend{one, two, three, four}
\end{axis}
\end{tikzpicture}%
%}
}

\begin{document}

\noindent\begin{tabularx}{\textwidth}{XX}
\clocks{20}{50}{30}{70} & \clocks{20}{50}{30}{70} \\
\clocks{20}{50}{30}{70} & \clocks{20}{50}{30}{70} \\
\end{tabularx}

\end{document}

enter image description here

Outside floating objects you can provide a caption (requested in a comment) using \captionof, provided by the caption or capt-of packages:

\documentclass[a4paper,11pt,twoside]{book}
\usepackage{caption}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{tabularx}

\pgfplotsset{compat=newest}

\newcommand{\clocks}[5][\linewidth]{%
% \resizebox{#5 \textwidth}{!}{
\begin{tikzpicture}
\begin{axis}[
  width=#1,
  ybar,
  enlargelimits=0.25,
%  legend style={at={(0.5,-0.15)},
%  anchor=north,legend columns=-1},
  ylabel={CPU clocks},
  symbolic x coords={one, two, three, four},
  xtick=data,
  nodes near coords,
  nodes near coords align={vertical},
]
\addplot coordinates {
(one, #2)
(two, #3)
(three, #4)
(four, #5)};
%\legend{one, two, three, four}
\end{axis}
\end{tikzpicture}%
%}
}

\begin{document}

\noindent\begin{tabularx}{\textwidth}{XX}
\clocks{20}{50}{30}{70}
\captionof{figure}{a description for the first plot} 
& 
\clocks{20}{50}{30}{70} 
\captionof{figure}{a description for the second plot} \\
\clocks{20}{50}{30}{70}
\captionof{figure}{a description for the third plot} 
 & \clocks{20}{50}{30}{70} 
\captionof{figure}{a description for the fourth plot} \\
\end{tabularx}

\end{document}

enter image description here

  • 1
    It looks a little clearer if you remove the horizontal bars and the hlines from the tabularx. There is also a little problem with the 70 sticking up too high, which is easy to fix by changing font. – Benjamin McKay Feb 4 '14 at 13:49
  • @BenjaminMcKay yes; I added an option without rules and preventing the overlapping. Thanks. – Gonzalo Medina Feb 4 '14 at 13:55
  • Thanks a lot, especially the last output is what I desired. Only problem: if I don't use figure, how can I add captures to the individual diagrams? – bogus Feb 4 '14 at 14:58
  • @bogus please see the last example code in my updated answer. – Gonzalo Medina Feb 4 '14 at 16:13
0

(!) use \noindent before the tabular to avoid overfull warnings.

Use \resizebox instead of figure. Set up its value. I'm using .45\textwidth.

\newcommand{\clocks}[4]{
\resizebox{.45\textwidth}{!}{\centering
\begin{tikzpicture}
\begin{axis}[
  ybar,
  ylabel={CPU clocks},
  symbolic x coords={one, two, three, four},
  xtick=data,
  nodes near coords,
  nodes near coords align={vertical},
]
\addplot coordinates {
(one, #1)
(two, #2)
(three, #3)
(four, #4)};
\end{axis}
\end{tikzpicture}
}
}

enter image description here

\noindent\begin{tabularx}{\textwidth}{|X|X|}
\hline
\clocks{20}{50}{30}{70} & \clocks{20}{50}{30}{70} \\
\hline
\clocks{20}{50}{30}{70} & \clocks{20}{50}{30}{70} \\
\hline
\end{tabularx}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.