Different alignment for table number and caption

I have a table whose table number should be right-aligned and its caption should be center-aligned on the next line like this:

                          Table 1
Caption
---------------------------------
|    |       |        |         |
---------------------------------


I found the caption package which is a powerful tool for changing caption styles, but it doesn't allow to solve my problem.

-

migrated from stackoverflow.comOct 23 '12 at 11:46

This question came from our site for professional and enthusiast programmers.

With the caption package, you may declare a new format that will add \hfill at the very start of the caption (hereby making its first line raggedright).

(In the following example, I also use the floatrow package to center the tabular material and to allow switching the caption's position from below to above.)

\documentclass{article}

\usepackage{caption}
\DeclareCaptionFormat{hfillstart}{\hfill#1#2#3\par}
\captionsetup[table]{format=hfillstart,labelsep=newline,justification=centering}

\usepackage{floatrow}
\floatsetup[table]{capposition=top}

\newcommand{\sometext}{Lorem ipsum dolor sit amet, consectetuer
adipiscing elit. Ut purus elit, vestibulum ut, placerat ac,
adipiscing vitae, felis. Curabitur dictum gravida mauris. Nam arcu
libero, nonummy eget, consectetuer id, vulputate a, magna.}

\begin{document}

This is normal text. \sometext

\begin{table}
(Tabular material)
\caption{This is a table caption. \sometext}
\end{table}

\end{document}


-
I believe that loading floatrow just to avoid \centering is exaggerated. :) –  egreg Oct 23 '12 at 12:10
@egreg It also allows to switch the caption's position. ;-) –  lockstep Oct 23 '12 at 12:12
@lockstep many thanks! it works like a charm! :) –  martemiev Oct 23 '12 at 13:55
@martemiev You're welcome -- and don't forget to accept my answer. ;-) –  lockstep Oct 23 '12 at 13:57