2

I few week ago, I learned subcaption figuras, with subcaption packages, and I like it very much.

Now I would like to caption/subcaption tabulars.

I was vieweing any issues and all have a table enviroment OVER the \begin{tabular} \end{tabular}

What´s the best and easy way to caption/subcaption a tabular??

like this....

\documentclass[12pt,a4paper,x11names]{article}
\usepackage[top=2.5cm, left=3.5cm, bottom=2.5cm, right=2.5cm]{geometry}

\usepackage{lipsum,mwe}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[spanish]{babel}

\begin{document}
Hello,\\

\begin{tabular}{|c|c|r|}
  \hline
  % after \\: \hline or \cline{col1-col2} \cline{col3-col4} ...
  a & e & 12 \\
  \hline
  f & g & 3 \\
  \hline
\end{tabular}

\end{document}
  • 2
    Use \begin{table} ... \end{table} around your tabular environment and insert a \caption or \subcaption as in the case with a figure – user31729 Jun 11 '14 at 12:56
  • 2
    Normally captions are only attached to "floats" (i.e. things like figures, tables, etc, that can 'float' to the top or bottom of a page). A tabular object is an inline object, not a float. As @ChristianHupfer says above, you can convert it to a float by wrapping the tabular object in a table environment. – John Wickerson Jun 11 '14 at 12:57
  • Thank you to the two. I use latex, but I can´t understand weell what float enviroments is, I need to get a strong base on latex. Thank you. So... I can use the subcaption package as I do with figures and images. Yes? – Mika Ike Jun 11 '14 at 13:03
  • 1
    @MikaIke: Yes, as long there is a \begin{table} ...\end{table} instead of \begin{figure}...\end{figure} – user31729 Jun 11 '14 at 13:05
1

Repeating the suggestions provided in the comments: Place the existing tabular environment inside a table environment. It is common -- though not mandatory -- to center the material inside a table environment. The command \centering achieves that objective. The table and figure environments are "floats", i.e., unless you explicitly instruct LaTeX not to do so, LaTeX will place them where it thinks it's best. For much more on floats and the LaTeX algorithm that determines float placement, see the posting How to influence the position of float environments like figure and table in LaTeX?

The table environment can be given a \caption and a \label. Assigning labels is useful if you want to cross-reference the table somewhere else in your document using the TeX/LaTeX command \ref.

If you do a lot of cross-referencing -- not only to tables, but to sections, figures, page numbers, etc -- you may want to get to know the cleveref package and its command \cref. For more on LaTeX's cross-referencing mechanisms and packages that provide enhanced cross-referencing macros see the posting Cross-reference packages: which to use, which conflict?

Here, finally, is your tabular environment encased in a table environment, with a couple of cross-referencing commands thrown in for good measure.

enter image description here

\documentclass[12pt,a4paper,x11names]{article}
\usepackage[vmargin=2.5cm, left=3.5cm, right=2.5cm] {geometry}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[spanish]{babel}
\usepackage[spanish]{cleveref} %% new

\begin{document}
Hello, \dots

\begin{table}[h]  %% new
\centering        %% new
\caption{A simple table} %% new
\label{tab:simple}       %% new
\begin{tabular}{|c|c|r|}
  \hline
  % after \\: \hline or \cline{col1-col2} \cline{col3-col4} ...
  a & e & 12 \\
  \hline
  f & g & 3 \\
  \hline
\end{tabular}
\end{table} %% new

Here's a cross-reference to table \ref{tab:simple}.  %% new

Here's a cross-reference to \cref{tab:simple}.  %% new
\end{document}
  • Thank you very much. Your answer was very usefull to me. one thing: I see in refferes thw use of fig:, tab: , sec: and I suposse is to keep order and know what type of object is (I like cref writes"figura 3" instead of only "3" :-) Yes. but... What are the advantages of use fig:, tab:? I know 2: ordering reading a .tex, and let cref insert the words "figure"/"table" but.. any other advantantages??? – Mika Ike Jun 12 '14 at 8:42
  • I think that you speack spanish :-) for the line \usepackage[spanish]{cleveref}. How can I change the word "Cuadro" with the word "Tabla"? in cref – Mika Ike Jun 12 '14 at 8:43
  • I was trying with \renewcommand{\listtablename}{Índice de tablas} \renewcommand{\tablename}{Tabla} but NoSuccess. I tried changing thesee words in gloss-spanish.ldf but.. No Success! – Mika Ike Jun 12 '14 at 8:53
  • Finally I get the solution of the previous question. the lines \renewcommand{\listtablename}{Índice de tablas} \renewcommand{\tablename}{Tabla} must go after \begin{document} – Mika Ike Jun 12 '14 at 9:00
  • @MikaIke - You could type \crefname{table}{Tabla}{Tablas} to change the name of tables. Using prefixes such as tab:, fig:, and sec: inside labels is strictly optional; it does tend to simplify keeping track of various labels... – Mico Jun 12 '14 at 11:24

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.