# Centering tables in document class apa6

I'm using the apa6 class in doc mode for my thesis.

Now, when I create a table and try to center it on a page, the table is centered alright. The caption, however, still floats on the left of the page where the table used to be.

This what it looks like:

I would like the caption to align with the left side of the table.

This is basicly what my .tex-file looks like:

\documentclass[doc]{apa6}

\usepackage[american,ngerman]{babel}
\usepackage[style=apa,sortcites=true,sorting=nyt]{biblatex}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\shorttitle{Some title}

\begin{document}

\begin{table}[htbp]
\center
\caption{Some caption}
\label{table1}
\begin{tabular}{lcc}
\toprule
Test1 & 1 & 2\\
Test2 & 1 & 2\\
Test3 & 1 & 2\\
\bottomrule
\end{tabular}
\end{table}

\end{document}

-
Rather than posting fragments, it's better to post a complete, compilable minimal example. Especially in this case, since the apa6 class requires \shorttitle to be used, so you can't just piece together your fragments. Also, you should use \centering not \center inside your tables. – Alan Munn Jan 25 '12 at 3:00
Welcome to TeX.SE! About the use of the apa6 document classs: Is this a requirement of your school or is this something you can select yourself? A design choice embedded in the apa6e class is that all float captions are typeset left-justified on two lines, with a line break between "Tabelle x" (in German because of your babel setting) and the actual text of the caption. In addition, the tabular materials should also be set left-justified, i.e., you shouldn't use the \centering command. If you don't like these design choices, a different document class may be better suited to your needs. – Mico Jan 25 '12 at 3:17
Hi Alan, thanks for the comment. I fixed my example and added a screenshot to clarify the problem. – crash Jan 25 '12 at 3:44
Hi Mico, I use apa6 because besides from the tables it works smoothly and does everything I need. I edited my question because it appears it was unclar. I want the captions to be left justified. It is their position relative to the table that is off. – crash Jan 25 '12 at 3:48

The caption package is loaded by the apa6 class, so you can use its commands to set the caption formatting. Adding:

\captionsetup{justification=centering}


will cause the caption to be centred. However, this doesn't seem to be the exact formatting you want. What you want a left aligned caption aligned to the natural width of the table. As Axel Sommerfeldt notes in the comments, there is an command in the caption package which allows you to place a table and its caption in a box. This will allow you to do what you want quite simply. The command has not yet made it into the documentation of caption (v3.2).

The syntax of this command is:

\captionbox[⟨list entry⟩]{⟨heading⟩}[⟨width⟩][⟨inner-pos⟩]{⟨contents⟩}


(quoting from the subcaption package by the same author:)

The arguments ⟨list entry⟩ & ⟨heading⟩ will be used for typesetting the \caption. ⟨width⟩ is the width of the resulting \parbox; the default value is the width of the contents.

⟨inner-pos⟩ specifies how the contents will be justified inside the resulting \parbox; it can be either c (for \centering), l (for \raggedright), r (for \raggedleft), or s (for no special justification). The default is c. (But you can use any justification defined with \DeclareCaptionJustification as well, e.g. centerlast.)

\documentclass[doc]{apa6}

\usepackage[american,ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\shorttitle{A title}
\begin{document}
Example of a table:

\begin{table}[htbp]
\centering
\captionbox{Some caption\label{table1}}{%
\begin{tabular}{lcc}
\toprule
Test1 & 1 & 2\\
Test2 & 1 & 2\\
Test3 & 1 & 2\\
\bottomrule
\end{tabular}
}
\end{table}
\end{document}


Unless you have very strict formatting requirements, I would simply stick to centred captions instead of doing this.

(Thanks to both Werner and Axel for their comments on this answer.)

-
Actually, apa6 already loads caption and sets \captionsetup{justification=raggedright}. So, you only need to use \captionsetup{justification=centering}. – Werner Jan 25 '12 at 2:59
Thanks, Werner, I'll adjust the answer. – Alan Munn Jan 25 '12 at 3:01
Thanks, Alan. I have edited my question because it seems it was unclear. Nonetheless, I've tried various options of the \captionsetup but none of them seems to do the trick. – crash Jan 25 '12 at 3:51
Instead of \setuptable one could use \captionbox{<caption>}{<table>}. Unfortunately this command isn't documented yet, but it's avail since caption v3.2 and have the same syntax as \subcaptionbox documented in the subcaption package documentation. – Axel Sommerfeldt Jan 25 '12 at 8:39
@AlanMunn The only difference is that \subcaptionbox typesets a sub-caption while \captionbox typesets a regular one, so your answer is fine. But maybe one should change "undocumented" to "not-yet-documented" because "undocumented" could yield to the wrong impression it's better to not using this command. But it's definitely ok to use it, the only reason why it's not documented yet is that I've a full-time job, a wife, and cats ;-) – Axel Sommerfeldt Jan 25 '12 at 20:33

You can load the zref package with the savepos module. This allows you to use \zsavepos{<label>} that saves the (x,y) coordinates of <label> on the page, retrievable via \zposx{<label>} and \zposy{<label>} in scaled points (or sp). This requires at least two compiles.

\documentclass[doc]{apa6}% http://ctan.org/pkg/apa6

%\usepackage[american,ngerman]{babel}% http://ctan.org/pkg/babel
%\usepackage[style=apa,sortcites=true,sorting=nyt]{biblatex}% http://ctan.org/pkg/biblatex
%\usepackage[utf8]{inputenc}% http://ctan.org/pkg/inputenc
%\usepackage[T1]{fontenc}% http://ctan.org/pkg/fontenc
\usepackage[savepos]{zref}% http://ctan.org/pkg/zref

\shorttitle{Some title}

\begin{document}

\begin{table}[htbp]
\centering
\begin{minipage}{\dimexpr\zposx{tabR}sp-\zposx{tabL}sp\relax}
\caption{Some caption}
\label{table1}
\zsavepos{tabL}\begin{tabular}{lcc}% Save tabular LEFT coordinate
\toprule
Test1 & 1 & 2\\
Test2 & 1 & 2\\
Test3 & 1 & 2\\
\bottomrule
\end{tabular}\zsavepos{tabR}% Save tabular RIGHT coordinate
\end{minipage}
\end{table}

\end{document}


You place a label to the left of your tabular environment (say tabL), and one to the right (say tabR). Then, \dimexpr\zposx{tabR}sp-\zposy{tabL}sp\relax gives the exact horizontal distance between tabL and tabR, which you use as the length restriction to a minipage.

You would need a different label for each tabular you want to force this way. It would be possible to automate this procedure by defining your own environment (say) mytable and mytabular, which you would use instead of table.

-