# Right align section title on odd pages & left align on even pages in Memoir class

Does anyone know how to left align section headings (so \raggedright) when they are on even pages (the normal behaviour for documents) but also to right align (so \raggedleft) them on odd pages (what I've shown in the MWE below)?

I've put a picture of the two pages of code below with an arrow showing what I want added to it.

I know \renewcommand\chapnamefont{\huge\bfseries\raggedleft} puts all section headings to the right of the page - but I need them to alternate on even and odd pages. I read somewhere this is easier in memoir than in the book class but I can't even begin to guess how to do it. Very new to memoir!

Thanks for any advice you may have.

\documentclass[12pt, openright]{memoir}

\usepackage{lipsum}

%% chapter always aligns right which is fine as chapters only appear on odd numbered pages
\renewcommand\chapnamefont{\huge\bfseries\raggedleft}

%% I want even page sections to be \raggedright and viceversa so they will match the headers (not shown in this MWE)

\begin{document}

\chapter{Name of chapter 1}
\section{sec 1}
\clearpage
\section{sec 2}

\end{document}


Disclaimer - I realise your question is about how to implement this using the memoir class, which has its own built in tools which match the functionality of packages like titlesec. But, until someone posts a memoir solution, here is a way to do it using titlesec.

\documentclass[12pt, openright]{memoir}
\usepackage{showframe} %<---- Just for testing

%% chapter always aligns right which is fine as chapters only appear on odd numbered pages
\renewcommand\chapnamefont{\huge\bfseries\raggedleft}

\usepackage{titlesec}
\titleformat{name=\section,page=odd}{\large\bfseries\raggedleft}{\thesection}{1em}{}
\titleformat{name=\section,page=even}{\large\bfseries\raggedright}{\thesection}{1em}{}

\begin{document}

\chapter{Name of chapter 1}
\section{sec 1}
\clearpage
\section{sec 2}

\end{document}


You are almost there --- use memoir's \checkoddpage macro. In your memoir preamble put:

% \strictpagecheck % uncomment if problems at new pages
\newcommand{\oddright}{\checkoddpage\ifoddpage\raggedleft\fi}

If you want a similar effect for \subsection then add
\setsubsecheadstyle{\oddright\large\bfseries}

See sections 18.12 and 6.6 in the manual (> texdoc memoir) for further information about odd/even page checking and changing the style of sectional headings.