4

I'm trying to create a table with a caption on top of it, with the caption centered to the table.

I have this code:

\documentclass[a4paper]{article}

\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage[colorinlistoftodos]{todonotes}
\usepackage{float}

\title{Your Paper}
\author{You}

\begin{document}
\maketitle

\begin{table}[H]
\caption{This is a caption}
\begin{tabular}{|c|c|}
\hline
a & 1\\ \hline
b & 2\\ \hline
c & 3\\ \hline
d & 4\\ \hline
\end{tabular}
\end{table}

\end{document}

What this does is create a table to the left of the paper (where I want it), but the caption appears in the center of the page, far from the table. I can center the table, of course, all looks fine, but I do need the table on the left of the page.

I searched around for similar problems, but what mostly found were people wanting to center tables, and I want quite the opposite, to uncenter the caption. Anyway, I did not find a solution to this problem so, if you can help, I appreciate it.

Also: I am working in writelatex.com, if this helps

4

The caption package provides the singlelinecheck key-value:

The standard LaTeX document classes (article, report, and book) automatically center a caption if it fits in one single line ... The caption package adapts this behavior and therefore usually ignores the justification & indention you have set with justification= & indention= in such case. But you can switch this special treatment of such short captions off with the option singlelinecheck=<bool>.

Using false, no, off or 0 for <bool> switches the extra centering off ... You switch the extra centering on again by using true, yes, on or 1 for <bool>. (The default is on.)

enter image description here

\documentclass{article}
\usepackage[singlelinecheck=false]{caption}% http://ctan.org/pkg/caption

\title{Your Paper}
\author{You}

\begin{document}
\maketitle

\captionof{table}{This is a caption}
\begin{tabular}{|c|c|}
  \hline
  a & 1 \\ \hline
  b & 2 \\ \hline
  c & 3 \\ \hline
  d & 4 \\
  \hline
\end{tabular}

\end{document}

I've dropped the use of float and opted to use caption's \captionof{table}{<caption>} which is similar to using the [H] float specifier. Depending on the location, you may have to issue a \nobreak to avoid \caption/tabular separation across a page break.

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.