GeneralGenerating different page layouts from the same source

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
robertjlee
Posts: 17
Joined: Thu Aug 07, 2008 7:51 pm

Generating different page layouts from the same source

Post by robertjlee »

What's the best way to generate two versions of a document, on different paper sizes, from the same source, using a custom class based on scrartcl?

Context: I have a number of different documents, and have to apply the same formatting to each of them for consistency. Because the format depends on the page size, I found a class to be more useful than a style.

I have used the following technique in the past, but it is no longer working for me after an update and switching to lualatex 1.14:

mwe.tex:

Code: Select all

\documentclass{mwe}
\begin{document}
.
\end{document}
mwe.cls:

Code: Select all

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{mwe}
% if letterleaf is defined in the Makefile, use letter paper options. Otherwise A4.
\newcommand{\ifletter}[2]{\ifcsname letterleaf\endcsname #1 \else #2 \fi}

\ifletter{
 % add to global options list, so it gets passed to typearea.
 \xdef\@classoptionslist{\@classoptionslist,paper=letter}
}{}
\LoadClass[12pt,DIV=15]{scrartcl}
Makefile:

Code: Select all

all : mwe-a4.pdf mwe-letter.pdf

mwe-a4.pdf : mwe.tex mwe.cls
	lualatex -interaction=nonstopmode -halt-on-error \
	--jobname=mwe-a4 mwe.tex

mwe-letter.pdf : mwe.tex mwe.cls
	lualatex -interaction=nonstopmode -halt-on-error \
	--jobname=mwe-letter \
	'\xdef\letterleaf{1}\input{mwe.tex}'
The idea is to set define the macro \letterleaf in the Makefile, then interrogate it in the .cls to append "paper=letter" to \@classoptionslist (the list of global class options), before loading scrartcl - as if it were passed in the optional argument to \documentclass.

I expect the above to produce two documents, one on A4 paper, and one on US Letter paper. Unfortunately, both documents have the default A4 paper size.

There are no unused global option warnings. If I add "paper=letter" to the \documentclass optional argument, it works, so I know the format is correct - and I can see it's adding the option to \@classoptionslist via tracing.

Do global options no longer use \@classoptionslist? Or is there a better way to achieve this? Thanks for any help.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

robertjlee
Posts: 17
Joined: Thu Aug 07, 2008 7:51 pm

Generating different page layouts from the same source

Post by robertjlee »

I found a workaround. I still have no idea why \@classoptionslist stopped working, but instead of setting the paper size, I simply let it default to A4 then set it before the document begins.

mwe.cls

Code: Select all

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{mwe}
% if letterleaf is defined in the Makefile, use letter paper options. Otherwise A4.
\newcommand{\ifletter}[2]{\ifcsname letterleaf\endcsname #1 \else #2 \fi}

\LoadClass[12pt,DIV=15]{scrartcl}

\ifletter{
  \KOMAoptions{paper=letter,DIV=15}
}{}
It does mean encoding the DIV value in two places (I could create a macro for it I suppose), but at least my immediate problem is solved.

I'm still puzzled as to why letter=a4 works as a global option if you changed it to \documentclass[letter=a4]{mwe}, but not as an argument passed via \@classoptionslist.
rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

Generating different page layouts from the same source

Post by rais »

\@classoptionslist is still defined in the LaTeX Kernel, AFAICS. Have you considered that
robertjlee wrote:

Code: Select all

\LoadClass[12pt,DIV=15]{scrartcl}
may overwrite this \@classoptionslist of yours?
I mean, you are telling to load class scrartcl with options `12pt,DIV=15', doesn't this set said \@classoptionslist anew (thus discarding what was in it before)?
One test that comes to mind: discard the options from \LoadClass and add them to your definition of \@classoptionslist.
Another test: from within a, say, test document, look what \@classoptionslist was set to:

Code: Select all

\makeatletter
\show\@classoptionslist
\makeatother
then you know for sure if paper=... is there---or not.

KR
Rainer
robertjlee
Posts: 17
Joined: Thu Aug 07, 2008 7:51 pm

Generating different page layouts from the same source

Post by robertjlee »

Thank you for the suggestions. I have it working, so this is an academic question only.

I've tried \LoadClass{scrartcl}, as you suggested, and \LoadClassWithOptions{scrartcl}; both produce only A4 documents.

When I include \show\@classoptionslist, it does include paper=letter for the letter version; however, that doesn't rule out a \def or \edef local reassignment in a group while the class/package is loading. Oddly, it also prevents any pages being produced, which I did not expect. (I also tried this with plain \LoadClass{scrartcl} and with \LoadClassWithOptions{scrartcl}, and got the same results).

As an aside, note that other global options (such as "draft") are working when specified on the document's \useclass line.

I suspect it has something to do with the internals of Koma-script: how scrartcl.cls passes the option to the typearea package. It has no special handling for "paper=", so it is relying on global options (via \@classoptionslist I believe). I think the "correct" way of implementing this (which does work) is to use \PassOptionsToPackage rather than reassigning the "private" macro:

Code: Select all

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{mwe}

% if letterleaf is defined in the Makefile, use letter paper options. Otherwise A4.
\newcommand{\ifletter}[2]{\ifcsname letterleaf\endcsname #1 \else #2 \fi}
\ifletter{
 % add to global options list, so it gets passed to typearea.
  \PassOptionsToPackage{paper=letter}{typearea}
}{}

\LoadClass[12pt,DIV=15]{scrartcl}
I'm much happier with the above solution, as it doesn't feel like it involves any workarounds (other than for TeX's general lack of program arguments).
Post Reply