# Ragged justification of captions depending on odd/even page

Recovering my knowledge of LaTex long gone, would appreciate your help on how to achieve raggedleft or raggedrigh justification on margin captions depending on the page we are. I think it is probably not that easy as I was thinking and there is some dependency on the information latex has when laying out the pages, but not sure. Please check the example below

\documentclass[twoside]{scrartcl}
\usepackage[outer=7cm, inner=3cm, marginparwidth=4cm, marginparsep=10mm]{geometry}

\usepackage[top]{mcaption}
\usepackage[font=footnotesize,normal]{caption}
\usepackage{graphicx}
\usepackage{mwe}

\DeclareCaptionJustification{raggedauto}{\ifodd\thepage \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}


I get:

and

As you can see I only have the right effect I want on C, because I forced a \newpage. On my original files, other types of sections also seem to reset "the page side" correctly. What can I change to correct picture B automatically? Thanks

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, the material contained in the underlying \insert (a TeX primitive) may float to another page, by definition of floats such as LaTeX's table and figure environments. But once packed in the \insert (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

1. Usually, two compilation runs are sufficient to have stable labels; LaTeX warns when more are needed, as you probably know.

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}
%\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}