[Tex/LaTex] Right Align Caption with singlelinecheck

captionsfloatshorizontal alignment

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}

Best Answer

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

Related Question