2

Question below has been solved. The script has been edited.

I have find a template of subtable from this forum, but I can´t run it without errors. How can I solve this?

Error msg:

! LaTeX Error: Environment subtable undefined.
! Missing number, treated as zero.
! Illegal unit of measure (pt inserted).
! LaTeX Error: \begin{table} on input line 164 ended by \end{subtable}.

MWE:

\documentclass{article}
\usepackage{subfig}
\usepackage{array, booktabs, caption}
\usepackage{multicol}
\usepackage{multirow}
\usepackage{xcolor}
\usepackage{subcaption}
\usepackage{pgfplots}
\usepackage{tikz}
\definecolor{bblue}{HTML}{4F81BD}
\definecolor{rred}{HTML}{C0504D}
\definecolor{ggreen}{HTML}{9BBB59}
\definecolor{ppurple}{HTML}{9F4C7C}

\begin{document}
\begin{table}   
\centering
\begin{subtable}{.5\textwidth}
\centering
\begin{tikzpicture}
    \begin{axis}[
        width  = 1.3*\textwidth,
        height = 8cm,
        enlarge y limits={upper, value=0.4},
        ymin=0,
        major x tick style = transparent,
        ybar=2*\pgflinewidth,
        bar width=20pt,
        ymajorgrids = true,
        ylabel = {\%},
        symbolic x coords={A,B,C},
        xtick = data,
        nodes near coords,
        scaled y ticks = false,
        enlarge x limits=0.25,
        ymin=0,
        legend cell align=left,
        legend style={
                at={(1,1.05)},
                anchor=south east,
                column sep=1ex
        }
    ]
        \addplot[style={bblue,fill=bblue,mark=none]}]
            coordinates {(A, 100) (B,100) (C,100)};

        \addplot[style={rred,fill=rred,mark=none}]
             coordinates {(A, 106) (B,113) (C,177)};
        \legend{MADYMO,Vivo}
    \end{axis}
\end{tikzpicture}
\caption{Phase 1}
\end{subtable}
\begin{subtable}{.5\textwidth}
\centering 
\begin{tikzpicture}
    \begin{axis}[
        width  = 1.3*\textwidth,
        height = 8cm,
        enlarge y limits={upper, value=0.4},
        ymin=0,
        major x tick style = transparent,
        ybar=2*\pgflinewidth,
        bar width=20pt,
        ymajorgrids = true,
        ylabel = {\%},
        symbolic x coords={A,B,C},
        xtick = data,
        nodes near coords,
        scaled y ticks = false,
        enlarge x limits=0.25,
        ymin=0,
        legend cell align=left,
        legend style={
                at={(1,1.05)},
                anchor=south east,
                column sep=1ex
        }
    ]
        \addplot[style={ggreen,fill=ggreen,mark=none]}]
            coordinates {(A, 100) (B,100) (C,100)};

        \addplot[style={ppurple,fill=ppurple,mark=none}]
             coordinates {(A, 98) (B,113) (C,178)};
        \legend{X,Y}
    \end{axis}
\end{tikzpicture}
\caption{Phase 2}
\end{subtable}
\caption{aaaaaaa}
\label{tab:EmMaxPE_bar_X}
\end{table}
\end{document}
  • 2
    Replace \usepackage{subfig} with \usepackage{subcaption}? Also, which template exactly were you referring to? – Torbjørn T. Nov 15 '16 at 17:26
  • In addition to loading the subcaption package, don't you also need to load one or more TikZ-related packages? Please specify them. – Mico Nov 15 '16 at 17:33
  • It worked, thank you!By the way, if I enlarge the table, how can i make sure it still centered? With template I mean this one:tex.stackexchange.com/questions/101320/grouped-bar-chart/… @TorbjørnT. – Vivian Nov 15 '16 at 17:39
  • @Vivian - Since the code you've provided a link to features the instruction width = 0.85*\textwidth, the tikzpicture environment will automatically fit inside the subtable. (The pictures don't look centered at first glance, but that's just because there's a % symbol located far to the right of the vertical axis.) – Mico Nov 15 '16 at 17:47
  • 1
    @Vivian : in this site, we keep questions separated from answers. Thus, in order to be understandable (and valuable) for future users, your should not update your question with the given answer. – ebosi Nov 15 '16 at 19:00
3

(Remark: I posted this question before the OP applied several substantial edits to the query. For instance, the subcaption environment wasn't being loaded in the initial form of the query, and there was no information about important aspects of the tikzpicture environments.)

To use subtable environments, you need to load the subcaption package. The subfig package does not provide a subtable environment.

In the following example, I've replaced the two tikzpicture environments with dummy tabular environments to simplify the exposition. (Also, you didn't indicate which tikz-related packages should be loaded or how rred, bblue etc. are defined.) Observe that you should insert a % (comment) character immediately after the first subtable environment if you want them to fit side-by-side.

enter image description here

\documentclass{article}
\usepackage{subcaption,booktabs}
\begin{document}

\begin{table}   
%\centering  <--- not needed!
\begin{subtable}{.5\textwidth}
\centering
   % dummy 'tabular' env.
   \begin{tabular}{lll}
   \toprule
   aaa bbb ccc\\
   \bottomrule
   \end{tabular}
\caption{Phase 1}
\end{subtable}% <--- new
\begin{subtable}{.5\textwidth}
\centering 
   % dummy 'tabular' env.
   \begin{tabular}{lll}
   \toprule
   xxx yyy zzz\\
   \bottomrule
   \end{tabular}
\caption{Phase 2}
\end{subtable}
\caption{aaaaaaa} \label{tab:EmMaxPE_bar_X}
\end{table}
\end{document}
  • You only need \usepackage{pgfplots} by the way, and if you want to test, change rred to red, and similar for green and blue. – Torbjørn T. Nov 15 '16 at 17:49
  • @TorbjørnT. - Indeed, the OP has since provided a link to the "template"; in that link, bblue, rred, etc are defined. Including the two tikzpicture environments instead of dummy tabular environments, while providing more visual excitement, changes nothing about needing to load the subcaption environment or needing to snug up the two subtable environments. – Mico Nov 15 '16 at 17:52
  • I do need the centering after \begin(table), otherwise the 2 tables will overlap each other. Due to the size of the table, I want to lie them in upper and lower part of the page, not side by side. But thank you for your suggestion. @Mico The caption is centered but not the table itself. – Vivian Nov 15 '16 at 18:03
  • @Vivian - You write, "I do need the centering after \begin(table), otherwise the 2 tables will overlap each other." That's not correct: The table environment contains two side-by-side subtable environments, which each have width 0.5\textwidth. As long as you insert the % character after the first subtable, as I suggest doing, there will be no overlap. If 0.5\textwidth simply is not big enough for your tikzpicture environments, you should consider loading the rotating package and using a sidewaystable environment instead of the current table environment. – Mico Nov 15 '16 at 18:47
  • @Mico LaTex has a lot of functions which I berely know! Thank you, I will check how to use them! – Vivian Nov 15 '16 at 18:53

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.