0

Description

For standard figure environments, the caption is centered if it fits in one line, and left-aligned if it does not.

enter image description here

However, when using the figure* environment from the sidenotes package, this behavior is not present: the captions are left-aligned in both cases.

enter image description here

Question

How can I have the figure* captions be centered if they fit one line (important: I want the line width to be the width of the text + the margin width, if possible. Lines longer than the text width, but less than text width + margin width should count as single line)? What I tried so far and what does not seem to work is

\usepackage[singlelinecheck=true]{caption}

or

\DeclareCaptionStyle{widefigure}{singlelinecheck=true}

Any help is appreciated.

MWE:

\documentclass[paper=a4]{scrartcl}
\usepackage[utf8]{inputenc}

% to fit both figures on one page
\usepackage[left=6cm, right=6cm, top=1cm, bottom=1cm]{geometry}
\usepackage{showframe}
\usepackage{sidenotes}
\usepackage{graphicx}

% neither option works
%\usepackage[singlelinecheck=true]{caption}
%\DeclareCaptionStyle{widefigure}{singlelinecheck=true}

\begin{document}

\begin{figure*}[h]
    \centering
    \includegraphics[width=\linewidth]{example-image-a}
    \caption{A figure}
    \label{fig:m}
\end{figure*}

\begin{figure*}[h]
    \centering
    \includegraphics[width=\linewidth]{example-image-a}
    \caption{A figure with very very very very long text it does not even stop? Will it ever stop? It should be long enough to not fit into a single line, at least I hope so.}
    \label{fig:m}
\end{figure*}

\begin{figure}[h]
    \centering
    \includegraphics[width=\linewidth]{example-image-a}
    \caption{A figure}
    \label{fig:m}
\end{figure}

\begin{figure}[h]
    \centering
    \includegraphics[width=\linewidth]{example-image-a}
    \caption{A figure with very very very very long text it does not even stop? Will it ever stop? It should be long enough to not fit into a single line, at least I hope so.}
    \label{fig:m}
\end{figure}

\end{document}

1
  • 1
    unrelated to captions but figure* can not be h only t and p so \begin{figure*}[h] prevents the figure being placed anywhere unless forced out by \clearpage. Commented Aug 11, 2021 at 21:49

2 Answers 2

1
+100

\DeclareCaptionStyle has an optional argument for the settings if a single line is detected. There you should set the centering.

caption uses \hsize for the measuring and centering, but adjustwidth doesn't set this. I'm using a hook here to set \hsize to \linewidth inside the figure*, this requires a new LaTeX.

\documentclass[paper=a4]{scrartcl}
\usepackage[utf8]{inputenc}

% to fit both figures on one page
\usepackage[left=6cm, right=6cm, top=1cm, bottom=1cm]{geometry}
\usepackage{showframe}
\usepackage{sidenotes}
\usepackage{graphicx}

\DeclareCaptionStyle{widefigure}[justification=centering]{font=footnotesize}
\AddToHook{env/figure*/begin}{\AddToHookNext{cmd/captionsetup/before}{\hsize\linewidth}}

\begin{document}

\begin{figure*}[h]
    \centering 
    \includegraphics[width=\linewidth]{example-image-a}
    \caption{A figure}
    \label{fig:m}
\end{figure*}

\begin{figure*}[h]
    \centering
    \includegraphics[width=\linewidth]{example-image-a}
    \caption{A figure with very very very very long text it does not even stop? Will it ever stop? It should be long enough to not fit into a single line, at least I hope so.}
    \label{fig:m}
\end{figure*}

\begin{figure}[h]
    \centering
    \includegraphics[width=\linewidth]{example-image-a}
    \caption{A figure}
    \label{fig:m}
\end{figure}

\begin{figure}[h]
    \centering
    \includegraphics[width=\linewidth]{example-image-a}
    \caption{A figure with very very very very long text it does not even stop? Will it ever stop? It should be long enough to not fit into a single line, at least I hope so.}
    \label{fig:m}
\end{figure}

\end{document}
1

I finally gave up on fixing sidenotes and simply created my own figure* environment.

\documentclass[paper=a4]{scrartcl}
\usepackage[utf8]{inputenc}

% to fit both figures on one page
\usepackage[left=6cm, right=6cm, top=1cm, bottom=1cm]{geometry}
\usepackage{showframe}
\usepackage{ifoddpage}
\usepackage{graphicx}

\newsavebox{\widefigurebox}

\renewenvironment{figure*}[1][tbp]{\figure[#1]%
  \begin{lrbox}{\widefigurebox}%
    \minipage{\dimexpr \textwidth+\marginparsep+\marginparwidth\relax}}%
{\endminipage\end{lrbox}\checkoddpage
  \ifoddpage
    \usebox{\widefigurebox}\hspace{\dimexpr -\marginparsep-\marginparwidth}%
  \else
    \hspace{\dimexpr -\marginparsep-\marginparwidth}\usebox{\widefigurebox}%
  \fi
  \endfigure}


\begin{document}

\begin{figure*}[h]
    \centering
    \includegraphics[width=\linewidth]{example-image-a}
    \caption{normal caption}
    \label{fig:m}
\end{figure*}

\begin{figure*}[h]
    \centering
    \includegraphics[width=\linewidth]{example-image-a}
    \caption{A figure with very very very very long text it does not even stop? Will it ever stop? It should be long enough to not fit into a single line, at least I hope so.}
    \label{fig:m}
\end{figure*}

\begin{figure}[h]
    \centering
    \includegraphics[width=\linewidth]{example-image-a}
    \caption{A figure}
    \label{fig:m}
\end{figure}

\begin{figure}[h]
    \centering
    \includegraphics[width=\linewidth]{example-image-a}
    \caption{A figure with very very very very long text it does not even stop? Will it ever stop? It should be long enough to not fit into a single line, at least I hope so.}
    \label{fig:m}
\end{figure}

\end{document}

This version keeps the caption inside the text area instead of expanding into the marginpar area. I also decided to use widefigure instead of figure*.

\documentclass[paper=a4]{scrartcl}
\usepackage[utf8]{inputenc}

% to fit both figures on one page
\usepackage[left=6cm, right=6cm, top=1cm, bottom=1cm]{geometry}
\usepackage{showframe}
\usepackage{ifoddpage}
\usepackage{graphicx}

\newsavebox{\widefigurebox}

\newenvironment{widefigure}[1][tbp]{\figure[#1]%
  \let\oldcaption=\caption
  \edef\oldwidth{\the\textwidth}%
  \checkoddpage
  \ifoddpage
    \renewcommand{\caption}[2][\empty]{\parbox{\oldwidth}{\ifx\empty##1\relax\oldcaption{##2}%
      \else\oldcaption[##1]{##2}\fi}\hfill}%
  \else
    \renewcommand{\caption}[2][\empty]{\hfill\parbox{\oldwidth}{\ifx\empty##1\relax\oldcaption{##2}%
      \else\oldcaption[##1]{##2}\fi}}%
  \fi
  \begin{lrbox}{\widefigurebox}%
    \minipage{\dimexpr \textwidth+\marginparsep+\marginparwidth}}% body here
{\endminipage\end{lrbox}%
  \ifoddpage
    \usebox\widefigurebox\hspace{\dimexpr -\marginparsep-\marginparwidth}%
  \else
    \hspace{\dimexpr -\marginparsep-\marginparwidth}\usebox\widefigurebox
  \fi
  \endfigure}

\begin{document}

\begin{widefigure}[h]
    \centering
    \includegraphics[width=\linewidth]{example-image-a}
    \caption{normal caption}
    \label{fig:m}
\end{widefigure}

\begin{widefigure}[h]
    \centering
    \includegraphics[width=\linewidth]{example-image-a}
    \caption{A figure with very very very very long text it does not even stop? Will it ever stop? It should be long enough to not fit into a single line, at least I hope so.}
    \label{fig:m}
\end{widefigure}

\begin{figure}[h]
    \centering
    \includegraphics[width=\linewidth]{example-image-a}
    \caption{A figure}
    \label{fig:m}
\end{figure}

\begin{figure}[h]
    \centering
    \includegraphics[width=\linewidth]{example-image-a}
    \caption{A figure with very very very very long text it does not even stop? Will it ever stop? It should be long enough to not fit into a single line, at least I hope so.}
    \label{fig:m}
\end{figure}

\end{document}

You must log in to answer this question.

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