2

I have a KOMA-script two-sided document and an enumerate environment (from package enumitem) which item labels are outside of the text body.

What I want is that all item labels are on the outer page side, so for odd pages on the right side of the text.

Consider below MWE:

\documentclass[a4paper,twoside]{scrreprt}
\usepackage[inline]{enumitem}
\usepackage{lipsum}  

\newenvironment{enumerateWithCode}[1]
{%
    \begin{enumerate}[
        leftmargin=0em,
        label={\small\textbf{#1.\arabic*)}},
        ref=#1.\arabic*]
}{%
    \end{enumerate}%
}

\begin{document}

\begin{enumerateWithCode}{RIGHT}
    \item \lipsum[1]
    \item \lipsum[1]
\end{enumerateWithCode}
\clearpage
\begin{enumerateWithCode}{LEFT}
    \item \lipsum[1]
    \item \lipsum[1]
\end{enumerateWithCode}

\end{document}

enter image description here

Using the environment option labelsep=\theEnumerateWithCodeLabelSep with

\newcommand{\theEnumerateWithCodeLabelSep}{%
    \dimexpr -5.5em -\textwidth\relax%
}

does move the item label to the right side (due to -\textwidth) but would mean I have to manually place separate environments whenever the item list expands multiple pages.

enter image description here

Therefore I have tried to use KOMA-scripts \Ifthispageodd

\newcommand{\theEnumerateWithCodeLabelSep}{%
    \Ifthispageodd{%
        \dimexpr -5.5em -\textwidth\relax%
    }{%
        .5em%
    }%
}

and package ifoddpage

\newcommand{\theEnumerateWithCodeLabelSep}{%
    \checkoddpage%
    \ifoddpage%
    \dimexpr -5.5em -\textwidth\relax%
    \else%
    .5em%
    \fi%
}

to dynamically/conditionally define the labelsep value with no success due to compilation errors.

Note: Whenever this may works out, I would also like to control the label to change the used braces.

FINAL SOLUTION based on accepted answer of user691586

Using labels without braces to make them work on even and odd pages and having them of same text length to provide somewhat equal spacing to the text body

\newcommand{\theEnumerateWithCodeLabelSep}{%
    \Ifthispageodd{\labelsep\dimexpr -2.1em -\labelwidth -\textwidth\relax}%
                  {\labelsep -.25em}%
}
% hacky solution for left and right alignment working on even and odd pages
\SetLabelAlign{enumerateWithCodeAlign}{\hss\llap{#1}\hfil} 
\newenvironment{enumerateWithCode}[1]
{%
    \let\originalitem\item%
    \def\item{%
        \Needspace*{\baselineskip}%
        \theEnumerateWithCodeLabelSep%
        \originalitem%
    }%
    \begin{enumerate}[
        leftmargin=0em,
        align=enumerateWithCodeAlign,
        label={\small\textbf{#1.\arabic*}},
        ref=#1.\arabic*] %% or \alph*
}{%
    \end{enumerate}%
    \def\item{\originalitem}%
}
1
  • 1
    This is a problem of expandability. The labelsep must clearly be usable in a TeX dimension assignment, and you need an expandable way of checking for page oddness.
    – user691586
    Apr 20, 2023 at 19:29

1 Answer 1

3

See code comments

\documentclass[a4paper,twoside]{scrreprt}
\usepackage[inline]{enumitem}
\usepackage{lipsum}  
\usepackage{needspace}
\newcommand{\updatelabelsep}{%
    \Ifthispageodd{\labelsep\dimexpr\itemindent-\labelwidth-\linewidth\relax}%
                  {\labelsep 0.5em}%
}
\newenvironment{enumerateWithCode}[1]
{%
    % maybe safe as long as not using * variants (inline) of enumerate
    \let\originalitem\item
    \def\item{\par
            % make sure this item has room, if not clear page while
            % keeping flushbottom in effect, so that the reference
            % inserted by \Ifthispageodd will be at top of next page
            % and the test of page parity on second LaTeX pass will be ok
            \Needspace*{\baselineskip}% maybe .5\baselineskip actually enough
            \updatelabelsep
            \originalitem
            }%
    \begin{enumerate}[%
        leftmargin=0em,
        label={\small\textbf{#1.\arabic*)}},
        ref=#1.\arabic*,
        labelwidth=6em,% make it large enough
        % itemindent=2em, % only to test
        ]
}{%
    \end{enumerate}%
}

\begin{document}
Skip to bottom
\vspace*{\dimexpr\textheight-4\baselineskip\relax}
\begin{enumerateWithCode}{ABCDE}
    \item random text
    \item random text
    \item random text
    \item random text
    \item random text
    \item random text
\end{enumerateWithCode}

\end{document}

list with page breaks and labels in margins

(obsolete first and second versions)

(second version, slightly better)

Try this

\documentclass[a4paper,twoside]{scrreprt}
\usepackage[inline]{enumitem}
\usepackage{lipsum}  

\newcommand{\updatelabelsep}{%
    \Ifthispageodd{\labelsep\dimexpr -5.5em -\textwidth\relax}%
                  {\labelsep .5em }%
}
\newenvironment{enumerateWithCode}[1]
{%
    \begin{enumerate}[%
        leftmargin=0em,
        label={\small\textbf{#1.\arabic*)}},
        ref=#1.\arabic*,
        ]
}{%
    \end{enumerate}%
}

\begin{document}

\begin{enumerateWithCode}{ABCDE}
    \updatelabelsep\item \lipsum[1]
    \updatelabelsep\item \lipsum[1]
    \updatelabelsep\item \lipsum[1]
    \updatelabelsep\item \lipsum[1]
    \updatelabelsep\item \lipsum[1]
    \updatelabelsep\item \lipsum[1]
\end{enumerateWithCode}
\clearpage
\begin{enumerateWithCode}{FGHIJ}
    \updatelabelsep\item \lipsum[1]
    \updatelabelsep\item \lipsum[1]
\end{enumerateWithCode}

\end{document}

enter image description here

If you don't want to have to add manually \updatelabelsep you can try this near end of preamble:

\let\originalitem\item
\def\item{\updatelabelsep\originalitem}

Then remove all occurrences of \updatelabelsep in the code above.

This looked a priori a bit dangerous (I see two \let\item\enit@endinbox in enumitem source code and did not search through this code extensively), but at least here appears to work. Use at own risk.

(initial answer)

Try this

\documentclass[a4paper,twoside]{scrreprt}
\usepackage[inline]{enumitem}
\usepackage{lipsum}  
%\usepackage{ifoddpage}

\newdimen\mydimen
\newenvironment{enumerateWithCode}[1]
{%
    \Ifthispageodd{\mydimen \dimexpr -5.5em -\textwidth\relax}%
                  {\mydimen .5em }%
    \begin{enumerate}[%
        leftmargin=0em,
        label={\small\textbf{#1.\arabic*)}},
        ref=#1.\arabic*,
        labelsep=\mydimen
        ]
}{%
    \end{enumerate}%
}

\begin{document}

\begin{enumerateWithCode}{RIGHT}
    \item \lipsum[1]
    \item \lipsum[1]
\end{enumerateWithCode}
\clearpage
\begin{enumerateWithCode}{LEFT}
    \item \lipsum[1]
    \item \lipsum[1]
\end{enumerateWithCode}

\end{document}

I presume \Ifthispageodd does not work purely by expansion so it could not be used as labelsep key value as this would end-up in some dimen (or skip, not checked) assignment by enumitem. So I use it before actually calling the enumitem environment.

Attention though that in case of multiple items there it is parity of page where environment starts which counts.

To fix this, one probably can do this but would need to presumably enter into details of how enumitem implements labelsep; which I did not do to avoid too hacky solution.

(end of obsolete versions)

10
  • But with this code, you still have to separate items to two different lists, if a list would expand multiple pages, wouldn't you?
    – cabohah
    Apr 21, 2023 at 7:01
  • @cabohah visiting the site only 11 minutes after your comment! Yes you are right, as I already pointed in my answer. However one can reset manually before each item. I will edit. Next step would be to find a hook point into enumitem's \item which I will not do (lack of time).
    – user691586
    Apr 21, 2023 at 7:14
  • @cabohah well I will have to look into \item because resetting the labelsep before\item does not have effect. It is too late enumitem has already frozen the thing.
    – user691586
    Apr 21, 2023 at 7:19
  • Nevertheless your basic idea was good. I've extended it a little bit (see my answer).
    – cabohah
    Apr 21, 2023 at 7:29
  • 1
    The parity detection in my answer via Koma \Ifthispageodd i s probably a bit fragile because it is done before the paragraph (with its label inserted via \everypar) starts and TeX decides to move or not to next page.
    – user691586
    Apr 21, 2023 at 7:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .