Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I would like to include 2 Tikz into one figure and caption each of the Tikz. I've explored subfigure but that did not seem to work. I'm trying to have the two diagrams appear side-by-side with a caption under each diagram. I appreciate any help rendered.

\begin{tikzpicture}[font=\small]
    \begin{axis}[
      ybar,
      height=8cm,
      width=6cm,
      bar width=15pt,
      xlabel={$c$},
      ylabel={Number of primes},
      ymin=0,
      ytick=\empty,
      xtick=data,
      axis x line=bottom,
      axis y line=left,
      enlarge x limits=0.2,
      symbolic x coords={0,1,2},
      xticklabel style={anchor=base,yshift=-\baselineskip},
      nodes near coords={\pgfmathprintnumber\pgfplotspointmeta}
    ]
      \addplot[fill=white] coordinates {
        (0,295)
        (1,175)
        (2,20)
      };
    \end{axis}
\end{tikzpicture}
%\caption{40-bit primes} \label{40bits}
\begin{tikzpicture}[font=\small]
    \begin{axis}[
      ybar,
      height=8cm,
      width=7cm,
      bar width=15pt,
      xlabel={$c$},
      ylabel={Number of primes},
      ymin=0,
      ytick=\empty,
      xtick=data,
      axis x line=bottom,
      axis y line=left,
      enlarge x limits=0.2,
      symbolic x coords={0,1,2,3,4},
      xticklabel style={anchor=base,yshift=-\baselineskip},
      nodes near coords={\pgfmathprintnumber\pgfplotspointmeta}
    ]
      \addplot[fill=white] coordinates {
        (0,182)
        (1,215)
        (2,75)
        (3,17)
        (4,1)
      };
    \end{axis}
\end{tikzpicture}
%\caption{41-bit primes} \label{41bits}
share|improve this question
    
Why not use subfig or subcaption instead? See subcaption vs. subfig: Best package for referencing a subfigure –  Adam Liter Mar 10 at 4:41
    
@Adam May I know which packages are they found in? –  BlackAdder Mar 10 at 4:44
    
Those are the names of the packages. subcaption is probably the best to use, as it is actively maintained. –  Adam Liter Mar 10 at 4:47
    
@Adam I've tried subfigure by naively enclosing tikzpicture and its caption into \begin{subfigure} and \end{subfigure}, but that didn't work. The complain is: Missing \endscname. –  BlackAdder Mar 10 at 4:52
1  
If you want a diagnosis for that, I would need to see a complete minimal working example (MWE), but, at any rate, I've provided an answer to your question that I think is what you want. –  Adam Liter Mar 10 at 4:57

1 Answer 1

up vote 5 down vote accepted

Here's a possibility that makes use of the \subcaption command of the subcaption package and the minipage environment (see p. 4 of the documentation; there are also dedicated subfigure and subtable environments that are explained on p. 5, if you prefer another possibility).

\documentclass{article}

\usepackage{pgfplots}
\usepackage{subcaption}

\begin{document}

\begin{figure}
\centering
\begin{minipage}[b]{.49\linewidth}
\centering
\begin{tikzpicture}[font=\small]
    \begin{axis}[
      ybar,
      height=8cm,
      width=6cm,
      bar width=15pt,
      xlabel={$c$},
      ylabel={Number of primes},
      ymin=0,
      ytick=\empty,
      xtick=data,
      axis x line=bottom,
      axis y line=left,
      enlarge x limits=0.2,
      symbolic x coords={0,1,2},
      xticklabel style={anchor=base,yshift=-\baselineskip},
      nodes near coords={\pgfmathprintnumber\pgfplotspointmeta}
    ]
      \addplot[fill=white] coordinates {
        (0,295)
        (1,175)
        (2,20)
      };
    \end{axis}
\end{tikzpicture}
\subcaption{40-bit primes}\label{40bits}
\end{minipage}
\begin{minipage}[b]{.49\linewidth}
\centering
\begin{tikzpicture}[font=\small]
    \begin{axis}[
      ybar,
      height=8cm,
      width=7cm,
      bar width=15pt,
      xlabel={$c$},
      ylabel={Number of primes},
      ymin=0,
      ytick=\empty,
      xtick=data,
      axis x line=bottom,
      axis y line=left,
      enlarge x limits=0.2,
      symbolic x coords={0,1,2,3,4},
      xticklabel style={anchor=base,yshift=-\baselineskip},
      nodes near coords={\pgfmathprintnumber\pgfplotspointmeta}
    ]
      \addplot[fill=white] coordinates {
        (0,182)
        (1,215)
        (2,75)
        (3,17)
        (4,1)
      };
    \end{axis}
\end{tikzpicture}
\subcaption{41-bit primes} \label{41bits}
\end{minipage}
\end{figure}

\end{document}

enter image description here


Edit

In response your question in the comment section, you can change the appearance of the \subcaption formatting by renewing \thesubfigure command and by making use of the \captionsetup command.

To achieve the result you want, add

\renewcommand\thesubfigure{Fig~\thefigure(\alph{subfigure})}
\captionsetup[sub]{labelformat=simple}

to your preamble.

enter image description here

share|improve this answer
    
Thanks for your help @Adam, one last question though, how can I label the captions Fig 1(a) and Fig 1(b) for example? –  BlackAdder Mar 10 at 5:07
    
@BlackAdder see my update –  Adam Liter Mar 10 at 5:34
    
I'm very very grateful! Thanks! –  BlackAdder Mar 10 at 5:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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