1

i am a complete beginner. i don't know much about latex. i need help with positioning images. basically, there's 6 figures and i want them to be subfigures, where two each are side by side. i am dropping my code below.

\begin{figure}[h]

\centering
\begin{subfigure}[t][0.49\textwidth]
    \centering
    \includegraphics[width=0.49\textwidth]{pt 900/p900.png}
    \caption{$p_T$ spectra of p at 900}
    \label{fig:1-a}
\end{subfigure}
\hfill
\begin{subfigure}[t][0.49\textwidth]
    \centering
    \includegraphics[width=0.49\textwidth]{pt 900/pbar900.png}
    \caption{$p_T$ spectra of pbar at 900 GeV}
    \label{fig:1-b}
\end{subfigure}


\caption{$p_T$ spectra of identified charged particles at 900 GeV}
\label{fig:1}
\end{figure}

\end{document}

i have repeated the subfigure thing 6 times. the problem is i first of all am not getting the desired reult. secondly, i am getting these two errors for every subfigure.

<to be read again> 
                   \protect 
l.34         \centering
                       
A number should have been here; I inserted `0'.
(If you can't figure out why I needed to see a number,
look up `weird error' in the index to The TeXbook.)



<to be read again> 
                   \protect 
l.34         \centering
                       
A number should have been here; I inserted `0'.
(If you can't figure out why I needed to see a number,
look up `weird error' in the index to The TeXbook.)

please please someone help me. i have tried everything but cant seem to figure it out. i urgently need this.
PS: my preamble

\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{float}
\usepackage{hyperref}

1 Answer 1

0

You're probably mixing the notation/syntax from other sub-float related packages. When using subcaption, it provides a subfigure environment which is similar to the subcaptionblock environment and takes a single, mandatory argument. You currently supply vertical position and width arguments as optional, which is the cause of the issue.

Use the following instead:

enter image description here

\documentclass{article}

\usepackage{graphicx,subcaption}

\begin{document}

\begin{figure}
  \begin{subfigure}{0.49\linewidth}
    \includegraphics[width=\linewidth]{example-image-a}
    \caption{$p_T$ spectra of p at 900}
    \label{fig:1-a}
  \end{subfigure}
  \hfill
  \begin{subfigure}{0.49\textwidth}
    \centering
    \includegraphics[width=\linewidth]{example-image-b}
    \caption{$p_T$ spectra of pbar at 900 GeV}
    \label{fig:1-b}
  \end{subfigure}

  \caption{$p_T$ spectra of identified charged particles at 900 GeV}
\end{figure}

\end{document}

Note the following:

  • Single, mandatory argument for subfigure that defines the width of the box that subfigure will be placed in (internally it will use subcaptionblock).

  • With the width of the block set (to 0.49\textwidth, say), you can just use width=\linewidth for the \includegraphics scaling and it will fill the entire width of the block.

  • It seems like you just want a space between the two subfigures, so no \centering needed, only \hfill.

1
  • thank you so much! that was a grreat help, seriously!!
    – Sana Khan
    Commented May 2 at 18:59

You must log in to answer this question.

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