0

As explained in this answer, for an Exercise, images can be put inside a center environment (not in a floating figure). That works well in the exercise main body.

But inside a \Question, the image caption is not centered:

result

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{graphicx}

\usepackage{caption} \usepackage{exercise}

\begin{document}

\begin{Exercise}[title=MWE, label=ex:mwe] In the main exercise text, the caption is correctly centered on the image. \begin{center} \captionsetup{type=figure} \includegraphics[width=0.8\textwidth]{example-image} % example-image visible with mwe installed \caption{Caption text, properly centered.} \end{center}

\Question{ Why inside a ``Question'', the caption is not centered on the image ? \begin{center} \captionsetup{type=figure} % placed here for hyperref \includegraphics[width=0.8\textwidth]{example-image} \caption{Caption text, shifted.} \end{center} }

\end{Exercise}

\end{document}

The caption documentation is clear: for a single line, the caption should always be centered.
exercise is not listed in the "Supported packages" section, but that seems normal, since exercise does not deal with captions explicitly.

Is it an issue in caption or exercise, or something I should learn ?
What's going on ?
How to fix this properly ?
Advices welcome.

ederag
  • 213
  • To be clear, the caption should be centered just like the image, since they are both under the same center environment. – ederag May 15 '21 at 15:55

2 Answers2

1

Center is a list and will nest with other lists (like Exercise). In other words, center is not centered wrt the page but relative to the \Question. \caption ignores lists.

So the question is, do you really want the caption centered relative to the image, or the image centered relative to the page?

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{graphicx}

\usepackage{caption} \usepackage{exercise} \usepackage{showframe}

\makeatletter \newcommand\fullwidth{\hskip-@totalleftmargin \linewidth=\textwidth} \makeatother

\begin{document}

\begin{Exercise}[title=MWE, label=ex:mwe]

\Question{ Here we center the caption relative to the image. \begin{center} \captionsetup{type=figure, position=below} % placed here for hyperref \includegraphics[width=0.8\textwidth]{example-image} \parbox{\linewidth}{\caption{Caption text.}} \rule{\linewidth}{1pt} \end{center} }

\Question{ Here we center the image relative to the page. \begin{center} \captionsetup{type=figure, position=below} % placed here for hyperref \fullwidth\includegraphics[width=0.8\textwidth]{example-image} \caption{Caption text.} \rule{\linewidth}{1pt} \end{center} }

\end{Exercise}

\end{document}

John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • Thanks ! Encapsulating the caption inside a \parbox{\linewidth}{ } fully solves the issue. The caption is then centered on the image. For an image belonging to a Question, it makes sense that it is centered on the text line (so your first solution). – ederag May 10 '21 at 15:48
  • Actually, when the documentation said "centered", and figure environment was not needed, I expected the centering to be relative to the current environment (center). Why is \caption relative to the page by default ? – ederag May 10 '21 at 16:02
  • If you put a float inside a list (which you should not do), it will still use the whole width. \caption expects to be inside a float. Apparently it goes to some effort, beyond, for example, \makebox[\textwidth]{...}. – John Kormylo May 10 '21 at 17:51
0

Your figures are perfectly centered, but you are optically mislead by the \parindent of your first sentence and the smaller textwidth of the question. I extended the respective texts. If you compile them, you should get the attached outcome:

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{graphicx}

\usepackage{caption} \usepackage{exercise}

\begin{document}

\begin{Exercise}[title=MWE, label=ex:mwe] In the main exercise text, the caption is correctly centered on the image. This as well is a longer line. \begin{center} \captionsetup{type=figure} \includegraphics[width=0.8\textwidth]{example-image} % example-image visible with mwe installed \caption{Caption text, properly centered.} \end{center}

\Question{ Why inside a ``Question'', the caption is not centered on the image? Why is this a shorter line? \begin{center} \captionsetup{type=figure} % placed here for hyperref \includegraphics[width=0.8\textwidth]{example-image} \caption{Caption text, shifted.} \end{center} }

\end{Exercise}

\end{document}

figure in question

C. Peters
  • 1,297
  • I don't believe to be "optically mislead". To be clear, my question is not about the images (they are perfectly centered). With my setup (texlive shipped with openSUSE Leap 15.2), your code yields also a shifted caption. No improvement. So your latex is better than mine. What's yours ? – ederag May 10 '21 at 11:40
  • 1
    I use the most recent TeXShop and compiled just with LaTeX. I now see your point. The caption is centered to the entire textwidth, including the number, at least in my output, even when I change the text of the second caption into the longer text of the first caption. I am not experienced enough to tell you how to measure the linewidth without the indent of the number of the question. – C. Peters May 10 '21 at 12:06
  • Looking more precisely, indeed, your caption is shifted as well, although it is less pronounced. So it is not solved by a more recent version. John Kormilo answer shows how to work around that. – ederag May 10 '21 at 16:49