1

I'd like to have the captionlabel (e.g. Figure 1) centered on a single line with the actual caption justified below. Something like this

example

As you can see, I've achieved what I want with the help of the caption package. However, this generates

LaTeX Error: Something's wrong--perhaps a missing \item.

for every figure. What am I doing wrong or how can I achieve this result without this error?

Something like

\usepackage[labelsep=newline,justification=centerfirst]

sort of achieves what I want and without errors, but the text is made to span the whole width of the textbox. (How do you call that, fully justified?) This often greatly decreases the legibility and I'd prefer not to use this option.

MWE:

\documentclass{article}

\usepackage[demo]{graphicx}
\usepackage{caption}

%\DeclareCaptionLabelFormat{center}{{\centering #1 #2}}
\DeclareCaptionFormat{center}{{\centering#1#2\\}#3}
\captionsetup{format=center,labelsep=none,justification=justified}

\begin{document}

\begin{figure}
    \centering
    \includegraphics[width=\textwidth]{}
    \caption{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin quis egestas massa, vitae imperdiet ligula. Curabitur porta luctus orci at congue. Cras luctus blandit velit, quis congue lectus ultricies vel. Quisque non eros ut ante tincidunt mattis sed quis nisi.}
\end{figure}

\end{document}
  • Does \DeclareCaptionFormat{center}{{\hfil#1#2\hfil}#3} works out for you? – Arash Esbati May 28 '15 at 10:06
2

\centerline works:

\documentclass{article}

\usepackage[demo]{graphicx}
\usepackage{caption}


\DeclareCaptionFormat{center}{{\centerline{#1#2}\\}#3}
\captionsetup{format=center,labelsep=none,justification=justified}

\begin{document}

\begin{figure}
    \centering
    \includegraphics[width=\textwidth]{}
    \caption{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin quis egestas massa, vitae imperdiet ligula. Curabitur porta luctus orci at congue. Cras luctus blandit velit, quis congue lectus ultricies vel. Quisque non eros ut ante tincidunt mattis sed quis nisi.}
\end{figure}

\end{document}

enter image description here

2

You can define a centerlabel format in the following way:

\documentclass{article}

\usepackage{graphicx}
\usepackage{caption}

\DeclareCaptionFormat{centerlabel}{%
  \sbox0{#3}%
  \makebox[\linewidth]{\scshape#1#2}\\
  \ifdim\wd0<\linewidth
    \makebox[\linewidth]{\unhbox0}%
  \else
    \unhbox0
  \fi
}
\captionsetup{format=centerlabel,labelsep=none,margin=2cm}

\begin{document}

\begin{figure}[htp]
\centering
\includegraphics[width=\textwidth,height=3cm]{example-image}

\caption{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin quis egestas massa, 
vitae imperdiet ligula. Curabitur porta luctus orci at congue. Cras luctus blandit velit, 
quis congue lectus ultricies vel. Quisque non eros ut ante tincidunt mattis sed quis nisi.}

\end{figure}

\begin{figure}[htp]
\centering
\includegraphics[width=\textwidth,height=3cm]{example-image}

\caption{Very short caption}

\end{figure}

\end{document}

enter image description here

  • I too agree that centerfirst is the way to go; however, why redefine it? Also, @egreg, could you kindly explain the difference between the existing definition and this one and how this one works? I'd appreciate it. Thanks! – Amar May 28 '15 at 11:02
  • 1
    @Amar I actually wasn't aware that a centerfirst style already existed! /hiding The main difference is that here I check whether the caption text fits in the linewidth and center it if so: justifying a short caption with this format would give funny results. – egreg May 28 '15 at 12:09
  • Ya, centerfirst is the only thing I use almost exclusively in terms of caption justification. And the "funny results" you are talking about, I get those too, many a times; so, I just adjust margin=x pt inside \captionsetup{} for every such "funny occurrence". A lay-man's troubleshoot! :) I think, your centerlabel format will actually be very useful for me in such cases. Many thanks! Also, how did you figure out that \wd0 controls the caption width? And is \unbox0 used to spew out caption contents inside a box of width \linewidth? – Amar May 28 '15 at 12:42
  • @egerg What is this /hiding you mention in your comment above? Or is it there by mistake?? – Amar May 28 '15 at 13:37
  • 1
    @Amar I didn't figure out anything: with \sbox0{#3} I typeset the caption text in box register 0, so I can access its width (without line breaks) as \wd0; then I can decide whether centering it with \makebox[\linewidth]{\unhbox0} or simply make a paragraph, with \unhbox0. The command \unhbox0 delivers the contents of box register 0, but unboxed, so it can be reflown. – egreg May 28 '15 at 16:22
0
\documentclass{article}

\usepackage[demo]{graphicx}
\usepackage{caption}

\DeclareCaptionFormat{center}{{\centerline{\textsc{#1}#2}\\}#3}
\captionsetup{format=center,labelsep=none,justification=justified}

\begin{document}

\begin{figure}
    \centering
    \includegraphics[width=\textwidth]{}
    \caption{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin quis egestas massa, vitae imperdiet ligula. Curabitur porta luctus orci at congue. Cras luctus blandit velit, quis congue lectus ultricies vel. Quisque non eros ut ante tincidunt mattis sed quis nisi.}
\end{figure}

\end{document}

Justified, centered, small-caps

  • This works, but as I stated I'd rather not use this option. I don't find the text as legible as my error generating example. – Johannes May 28 '15 at 10:17
  • Missed that, edited answer. – Daniel May 28 '15 at 10:21
0

Try this. I have removed the \\ used \hfil and \par in

\DeclareCaptionFormat and added width in the 
\captionsetup

Fullcode:

\documentclass{article}

\usepackage[demo]{graphicx}
\usepackage{caption}

%\DeclareCaptionLabelFormat{center}{{\centering #1 #2}}
\DeclareCaptionFormat{center}{{\hfil\textsc{#1}#2\hfil}\par#3}
\captionsetup{format=center,labelsep=none,justification=justified, width=.8\textwidth}

\begin{document}

\begin{figure}
\centering
\includegraphics[width=\textwidth]{}
\caption{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin quis egestas massa, vitae imperdiet ligula. Curabitur porta luctus orci at congue. Cras luctus blandit velit, quis congue lectus ultricies vel. Quisque non eros ut ante tincidunt mattis sed quis nisi.}
\end{figure}

\end{document}

enter image description here

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.