5

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:

Table with incorrectly positioned caption

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}
  • 2
    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. – crsh 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. – crsh Jan 25 '12 at 3:48
9

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⟩}
\captionbox*{⟨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}

enter image description here

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, 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. – crsh Jan 25 '12 at 3:51
  • @crash I've updated my answer. Seems like a lot of work. You might want to check out the other answers to the question I linked to. – Alan Munn Jan 25 '12 at 4:39
  • 1
    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. – user2574 Jan 25 '12 at 8:39
  • 2
    @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 ;-) – user2574 Jan 25 '12 at 20:33
2

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.

Caption sized to width of table via zref's \zsavepos

\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.

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.
https://tex.stackexchange.com/questions/42209/centering-tables-in-document-class-apa6
tex.stackexchange.com
crawl=CC-MAIN-2020-10
164995
application/http; msgtype=response
sha1:NKCJTUILR3QJN2UIAEHD6TTGZFB3YKTR
<urn:uuid:02cbe0ad-975e-450a-8307-18f3f603b4be>
2020-02-26T20:40:12Z
151.101.1.69
text/html
sha1:5ZIHGO555TXTWE3JHFN4VBNAUOFJIHIT
<urn:uuid:3dc68eef-2668-4fe4-9afa-fcc1568e4553>
https://tex.stackexchange.com/questions/42209/centering-tables-in-document-class-apa6
response
<urn:uuid:ebe01d7f-0085-4266-89fb-7cd503441234>
isPartOf: CC-MAIN-2020-10
publisher: Common Crawl
description: Wide crawl of the web for February 2020
operator: Common Crawl Admin (info@commoncrawl.org)
hostname: ip-10-67-67-36.ec2.internal
software: Apache Nutch 1.16 (modified, https://github.com/commoncrawl/nutch/)
robots: checked via crawler-commons 1.1-SNAPSHOT (https://github.com/crawler-commons/crawler-commons)
format: WARC File Format 1.1
conformsTo: http://iipc.github.io/warc-specifications/specifications/warc-format/warc-1.1/