9

I've been happily using the multicol package to layout a set of documents that are generated for multiple output formats from the same source. By necessity this moves same code from the document source to a style file. For example I need to be able to make the choice to prepend a title or not from the style package. Instead of:

\begin{document}
\maketitle

I need to be able to run the equivalent of:

\AtBeginDocument{\maketitle}
\begin{document}

This seems simple enough, but multicol wreaks havoc on breaking mdframed boxes and apparently there is no way to fix that. So I've been trying to adapt to using the twocolumn layout mode instead. This fixes the box breaking issues, but introduces new ones. First up I tried adding the option to my geometry package, which I was already using. This had the odd effect of putting my title inside a column, which is not what I was after:

\documentclass{scrartcl}
\usepackage[twocolumn]{geometry}
\usepackage{lipsum}
\title{Title}
\author{Me}
\AtBeginDocument{\maketitle}
\begin{document}
\lipsum[1-3]
\end{document}

geomentry columns

So I tried using the option of the document class instead.

\documentclass[twocolumn]{scrartcl}
\usepackage{geometry}
\usepackage{lipsum}
\title{Title}
\author{Me}
\AtBeginDocument{\maketitle}
\begin{document}
\lipsum[1-3]
\end{document}

This produces an error:

! LaTeX Error: Missing \begin{document}.See the LaTeX manual or LaTeX Companion for explanation.Type H for immediate help.... \begin{document}

Strangely, running \maketitle manually solves this error:

\documentclass[twocolumn]{scrartcl}
\usepackage{geometry}
\usepackage{lipsum}
\title{Title}
\author{Me}
\AtBeginDocument{}
\begin{document}
\maketitle
\lipsum[1-3]
\end{document}

documentclass columns

And in this version my title spans the columns like I want it to, but of course I can't properly control in which editions there should be a title in the first place, which makes it useless to me.

How can I either ① set the columns with geometry but have the title span both columns or ② set them with the document class options but still be able to use \AtBeginDocument?

1
  • \AtBeginDocument still does code not meant for typesetting, before `\begin{document}.
    – user31729
    Mar 30, 2015 at 10:21

2 Answers 2

13

\AtBeginDocument is the wrong hook for typeset material. This hook is still executed in the preamble. Package etoolbox provides \AfterEndPreamble, which is executed at the very end of \begin{document}, thus document material is allowed here:

\documentclass[twocolumn]{scrartcl}
\usepackage{geometry}
\usepackage{etoolbox}
\usepackage{lipsum}
\title{Title}
\author{Me}
\AfterEndPreamble{\maketitle}
\begin{document}
\lipsum[1-3]
\end{document}
6

One way is to use \xapptocmd from xpatch package to append some command after \begin{document} (short form \document)

\AtBeginDocument is not meant for typesetting material stuff since it expands code before \begin{document} comes into action

Another approach is shown in the answer by Heiko Oberdiek.

\documentclass[twocolumn]{scrartcl}
\usepackage{geometry}
\usepackage{xpatch}
\usepackage{lipsum}
\title{Title}
\author{Me}
%\AtBeginDocument{\maketitle}
\xapptocmd{\document}{\maketitle}{}{}
\begin{document}
\lipsum[1-3]
\end{document}

You must log in to answer this question.

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