5

I use singlelinecheck=true with the caption package to get small captions centered. However, I would like to be able to also get the single line captions to be left or right aligned. Getting them left aligned is obtained via

\captionsetup{singlelinecheck=false}%

as I have done in Figure 1. And if the caption was longer things would still work in this case.

How do get the single line caption of Figure 2 to be right aligned (but only if it is a single line caption)?

enter image description here

Code:

\documentclass{article}
\usepackage{showframe}
\usepackage{graphicx}

\usepackage{caption}
\captionsetup{%
    format=plain,%
    textformat=period,
    justification=RaggedRight,
    singlelinecheck=true,
}%

\begin{document}
\noindent
\begin{minipage}{0.5\linewidth}
    \centering
    \includegraphics[width=0.95\linewidth]{example-image}%
    \captionsetup{width=0.95\linewidth}%
    \captionsetup{singlelinecheck=false}%
    \captionof{figure}{Caption on Left}%
\end{minipage}%
\begin{minipage}{0.5\linewidth}
    \centering
    \includegraphics[width=0.95\linewidth]{example-image}%
    \captionsetup{width=0.95\linewidth}%
    \captionof{figure}{Caption on Right}%
\end{minipage}%

\medskip\noindent
\begin{minipage}[t]{0.5\linewidth}
    \centering
    \includegraphics[width=0.95\linewidth]{example-image}%
    \captionsetup{width=0.95\linewidth}%
    \captionof{figure}{Caption Centered}%
\end{minipage}%
\begin{minipage}[t]{0.5\linewidth}
    \centering
    \includegraphics[width=0.95\linewidth]{example-image}%
    \captionsetup{width=0.95\linewidth}%
    \captionof{figure}{Caption taking up full width}%
\end{minipage}%
\end{document}
4
  • Like this \captionsetup{justification=raggedleft, singlelinecheck=false } ?
    – Bobyandbob
    Apr 2, 2017 at 11:37
  • @Bobyandbob: But that does not produce correct results if the caption is longer than a single line. See what it does to the caption of Figure 4. Apr 2, 2017 at 11:48
  • Is there a way to do this exact same thing, but for text outside a \caption? For example, to force a minipage filled with text to behave like this?
    – Jim
    May 20, 2020 at 3:10
  • @Jim As that is unrelated to captions, it would be better if you posted a new question. May 20, 2020 at 5:51

2 Answers 2

3

You can use the optional argument of \DeclareCaptionStyle to define the special behavior of a onelined caption for an own caption style:

\documentclass{article}
\usepackage{showframe}
\usepackage{graphicx}

\usepackage{caption}

\DeclareCaptionStyle{mystyle}
  {format=plain,%
    textformat=period,
    justification=RaggedRight,
    singlelinecheck=true,
  }% all captions are left aligned

\DeclareCaptionStyle{singlelinecentered}
  [justification=Centering]% centered if single line and no `singlelinecheck=false`
  {style=mystyle}% other captions are left aligned

\DeclareCaptionStyle{singlelineraggedleft}
  [justification=RaggedLeft]% right aligned if single line and no `singlelinecheck=false`
  {style=mystyle}% other captions are left aligned

\captionsetup{style=singlelineraggedleft}

\begin{document}
\noindent
\begin{minipage}{0.5\linewidth}
    \centering
    \includegraphics[width=0.95\linewidth]{example-image}%
    \captionsetup{width=0.95\linewidth}%
    \captionsetup{singlelinecheck=false}%
    \captionof{figure}{Caption on Left}%
\end{minipage}%
\begin{minipage}{0.5\linewidth}
    \centering
    \includegraphics[width=0.95\linewidth]{example-image}%
    \captionsetup{width=0.95\linewidth}%
    \captionof{figure}{Caption on Right}%
\end{minipage}%

\medskip\noindent
\begin{minipage}[t]{0.5\linewidth}
    \centering
    \includegraphics[width=0.95\linewidth]{example-image}%
    \captionsetup{width=0.95\linewidth,style=singlelinecentered}%
    \captionof{figure}{Caption Centered}%
\end{minipage}%
\begin{minipage}[t]{0.5\linewidth}
    \centering
    \includegraphics[width=0.95\linewidth]{example-image}%
    \captionsetup{width=0.95\linewidth}%
    \captionof{figure}{Caption taking up full width}%
\end{minipage}%
\end{document}

enter image description here

1
  • This works well. If it is easy, can you include an option where the caption is right aligned with a specfic width. Eg, in Figure 2, how do I get the caption right aligned when the option \captionsetup{width=4.0cm} is also applied. Apr 3, 2017 at 7:45
1

An easy solution with floatrow:

\documentclass{article}
\usepackage{showframe}
\usepackage{graphicx}
\usepackage{floatrow, caption}

\captionsetup{%
    format=plain,%
    textformat=period,
    justification=RaggedRight,
    singlelinecheck=true,
}%
\renewcommand\ShowFrameLinethickness{0.3pt}

\begin{document}

\begin{figure}[!htb]
\centering\floatsetup{floatrowsep=qquad}
\captionsetup{font=small}
\begin{floatrow}\captionsetup{singlelinecheck=false}
    \ffigbox[\FBwidth]{%
    \includegraphics[width=0.4\textwidth]{example-image}}%
    {\caption{Caption on Left}}%
%%%
    \ffigbox[\FBwidth]{%
    \includegraphics[width=0.4\textwidth]{example-image}}%
    {\captionsetup{justification=raggedleft}\caption[]{Caption on Right}}%
\end{floatrow}%
\par
\vspace{3ex}
%%%%%%
\begin{floatrow}
    \ffigbox[\FBwidth]{%
 \includegraphics[width=0.4\textwidth]{example-image}}%
 { \caption{Caption Centered}}%
 %%%
    \ffigbox[\FBwidth]{%
 \includegraphics[width=0.4\textwidth]{example-image}}%
 {\captionsetup{format=hang}\caption[]{Long caption taking up full width}}%
\end{floatrow}%
\end{figure}

\end{document} 

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .