Ragged justification of captions depending on odd/even page
The KOMA-Script classes provide \Ifthispageodd
:
\documentclass[twoside]{scrartcl}
%\providecommand*\Ifthispageodd{\ifthispageodd}% needed up to and including KOMA-Script version 3.27, see https://komascript.de/faq_deprecatedif
\usepackage[outer=7cm, inner=3cm, marginparwidth=4cm, marginparsep=10mm]{geometry}
\usepackage[top]{mcaption}
\usepackage[font=footnotesize,normal]{caption}
\usepackage{graphicx}
\usepackage{mwe}
\DeclareCaptionJustification{raggedauto}{\Ifthispageodd{\raggedright}{\raggedleft}}% <- changed
\captionsetup{labelfont={bf}, font={it,footnotesize}, justification=raggedauto, singlelinecheck=false, position=above}
\begin{document}
\begin{figure}[htb]
\begin{margincap}
\caption{This is my caption}
\includegraphics[width=\textwidth]{example-image-a}
\end{margincap}
\end{figure}
\begin{figure}[htb]
\begin{margincap}
\caption{This is my caption}
\includegraphics[width=\textwidth]{example-image-b}
\end{margincap}
\end{figure}
% \newpage% <- not needed
\begin{figure}[htb]
\begin{margincap}
\caption{This is my caption}
\includegraphics[width=\textwidth]{example-image-c}
\end{margincap}
\end{figure}
\end{document}
Run twice to get
Package scrextend
also provides \Ifthispageodd
. So you can use it with a standard class, too:
\documentclass[twoside
,11pt% <- added, same fontsize as default for KOMA-Script classes
]{article}
\usepackage{scrextend}% <- added, provides \Ifthispageodd
%\providecommand*\Ifthispageodd{\ifthispageodd}% needed up to and including KOMA-Script version 3.27, see https://komascript.de/faq_deprecatedif
\usepackage[outer=7cm, inner=3cm, marginparwidth=4cm, marginparsep=10mm]{geometry}
\usepackage[top]{mcaption}
\usepackage[font=footnotesize,normal]{caption}
\usepackage{graphicx}
\usepackage{mwe}
\DeclareCaptionJustification{raggedauto}{\Ifthispageodd{\raggedright}{\raggedleft}}% <- changed
\captionsetup{labelfont={bf}, font={it,footnotesize}, justification=raggedauto, singlelinecheck=false, position=above}
\begin{document}
\begin{figure}[htb]
\begin{margincap}
\caption{This is my caption}
\includegraphics[width=\textwidth]{example-image-a}
\end{margincap}
\end{figure}
\begin{figure}[htb]
\begin{margincap}
\caption{This is my caption}
\includegraphics[width=\textwidth]{example-image-b}
\end{margincap}
\end{figure}
% \newpage% <- not needed
\begin{figure}[htb]
\begin{margincap}
\caption{This is my caption}
\includegraphics[width=\textwidth]{example-image-c}
\end{margincap}
\end{figure}
\end{document}
Welcome to TeX.SE!
When TeX processes your floating material (inside figure
environments), it expands \thepage
to the number of the page it is currently working on. However, this material may float to another page, by definition of floats such as LaTeX's table
and figure
environments. But once packed in the figure (after TeX has read it but before he's decided where to put it), the material where you used \thepage
has already been expanded, in a similar way as what you get after TeX has processed an \hbox
or a \vbox
command. As a result, your \ifodd
conditional is not present anymore, one of the two code paths has been left and the other discarded, all this before we know where the floating material will be eventually put. Precisely, the \ifodd
conditional has been expanded according to the page TeX was working on when he read the figure
material, not to the page where that material will be typeset in the end.
esdd gave you a solution using a command from KOMA-Script. Here is another one that should work with any class. It uses the changepage
package with option strict
. Once the package has been loaded, the only thing you have to do is to change your \DeclareCaptionJustification{raggedauto}{...}
like this:
\DeclareCaptionJustification{raggedauto}{%
\checkoddpage
\ifoddpage \raggedright \else \raggedleft \fi
}
The trick here that avoids the aforementioned problem is that \checkoddpage
automatically places a label a the point where it is used, and \ifoddpage
acts according to the page number in this label, not to the number of the page TeX is working on when reading this material. Therefore, the label holds the page number where the figure
has floated after the most recent compilation run,1 which is precisely what you want. Full code:
\documentclass[twoside]{scrartcl}
\usepackage[strict]{changepage}
\usepackage[outer=7cm, inner=3cm, marginparwidth=4cm,
marginparsep=10mm]{geometry}
\usepackage[top]{mcaption}
\usepackage[font=footnotesize,normal]{caption}
\usepackage{graphicx}
\usepackage{mwe}
\DeclareCaptionJustification{raggedauto}{%
\checkoddpage
\ifoddpage \raggedright \else \raggedleft \fi
}
\captionsetup{labelfont={bf}, font={it,footnotesize},
justification=raggedauto, singlelinecheck=false, position=above}
\begin{document}
\begin{figure}[htb]
\begin{margincap}
\caption{This is my caption}
\includegraphics[width=\textwidth]{example-image-a}
\end{margincap}
\end{figure}
\begin{figure}[htb]
\begin{margincap}
\caption{This is my caption}
\includegraphics[width=\textwidth]{example-image-b}
\end{margincap}
\end{figure}
\newpage
\begin{figure}[htb]
\begin{margincap}
\caption{This is my caption}
\includegraphics[width=\textwidth]{example-image-c}
\end{margincap}
\end{figure}
\end{document}
On page 1:
On page 2:
Footnote
- Usually, two compilation runs are sufficient to have stable labels; LaTeX warns when more are needed, as you probably know.