1

I have completed the following table below. However now I am trying to give the table a caption underneath, I just want to caption it Table 1: Variables. However when I try it I am running into problems, looking for some help thanks! I have included a screenshot

enter image description here

\documentclass{article}
\usepackage{graphicx}
\usepackage{booktabs, multirow}
\usepackage{siunitx}
\usepackage{amsmath}
\setlength{\extrarowheight}{2pt}
\usepackage{tabularx}
\begin{document}
{\footnotesize
\noindent\begin{tabularx}{\linewidth}{p{1.45cm}>{$}c<{$}Xlp{1.5cm}} \toprule
& \text{Symbol}
    & \hfil Description                   & Unit     & Constraint\\
\midrule
Variables
& S_H & Number of susceptible humans & number   & $S_H \geq 0 $ \\
& I_H & Number of infected humans & number   & $I_H \geq 0 $ \\
& R_H & Number of recovered humans & number   & $R_H \geq 0 $ \\
& V_H & Number of vaccinated humans & number& $ V_H \geq 0$ \\
& N_H & Number of total human population & number   & $N_H > 0 $ \\
& S_M & Number of susceptible mosquitoes & number   & $S_M \geq 0 $ \\
& E_M & Number of exposed mosquitoes & number   & $E_M \geq 0 $ \\
& I_M & Number of infected mosquitoes & number   & $I_M \geq 0 $ \\
& N_M & Number of total mosquito population & number   & $N_M > 0 $ \\
\bottomrule
\end{tabularx}
}
\end{document}
user123
  • 147
  • 5

1 Answers1

2

Use a table environment and inside it the \caption command. To reference it later use \label{...} inside of \caption (make sure to not introduce spurious spaces here), and use \ref{...} to reference it (\ref will produce only the number). Referencing needs at least two LaTeX runs.

\documentclass{article}
\usepackage{graphicx}
\usepackage{booktabs, multirow}
\usepackage{siunitx}
\usepackage{amsmath}
\setlength{\extrarowheight}{2pt}
\usepackage{tabularx}
\begin{document}
\begin{table}
\footnotesize
\noindent\begin{tabularx}{\linewidth}{p{1.45cm}>{$}c<{$}Xlp{1.5cm}} \toprule
& \text{Symbol}
    & \hfil Description                   & Unit     & Constraint\\
\midrule
Variables
& S_H & Number of susceptible humans & number   & $S_H \geq 0 $ \\
& I_H & Number of infected humans & number   & $I_H \geq 0 $ \\
& R_H & Number of recovered humans & number   & $R_H \geq 0 $ \\
& V_H & Number of vaccinated humans & number& $ V_H \geq 0$ \\
& N_H & Number of total human population & number   & $N_H > 0 $ \\
& S_M & Number of susceptible mosquitoes & number   & $S_M \geq 0 $ \\
& E_M & Number of exposed mosquitoes & number   & $E_M \geq 0 $ \\
& I_M & Number of infected mosquitoes & number   & $I_M \geq 0 $ \\
& N_M & Number of total mosquito population & number   & $N_M > 0 $ \\
\bottomrule
\end{tabularx}
\caption{A table\label{tab:tabel}}
\end{table}

If you want to reference it, use \ref{tab:tabel}.
\end{document}
Skillmon
  • 60,462
  • I seem to be getting some errors from your solution – user123 Apr 01 '18 at 17:23
  • 1
    @user123 then it seems you're doing something wrong. Please be more specific. What is the error message you get? Are you sure it is related to this answer? Did you try the above code on its own and still get an error? – Skillmon Apr 01 '18 at 17:24
  • i am getting a error from your \caption{A table... line. – user123 Apr 01 '18 at 17:26
  • @user123 Did you place everything inside a table environment and the \caption outside of tabularx but inside table (tabularx being inside the table environment, too)? – Skillmon Apr 01 '18 at 17:27
  • my error is saying the caption is outside the float – user123 Apr 01 '18 at 17:29
  • 1
    @user123 then you did not put the table environment around your tabularx and \caption. In the code above there is a \begin{table} ... \end{table} block around tabularx and \caption. – Skillmon Apr 01 '18 at 17:30
  • my mistake, i made a mistake when copying and pasting your code. Thanks! – user123 Apr 01 '18 at 17:32