TransWikia.com

Ragged justification of captions depending on odd/even page

TeX - LaTeX Asked by user2123288 on February 16, 2021

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}{ifoddthepage 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:
enter image description here

and

enter image description here

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

2 Answers

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:

Page 1


On page 2:

Page 2


Footnote

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

Correct answer by frougon on February 16, 2021

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

enter image description here


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}

Answered by esdd on February 16, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP