Document ClassesSwitch to one column in two-columned document

Information and discussion about specific document classes and how to create your own document classes.
Post Reply
falko
Posts: 4
Joined: Wed Oct 24, 2012 1:08 pm

Switch to one column in two-columned document

Post by falko »

Hi,

I'm trying to create a document class for two-column articles but with one-column title, authors, abstracts etc. Therefore I tried the (well-known?) solution from the UK TeX FAQ.

Latex, however, yields a "missing \begin{document}" error. Ignoring the error yields a perfect result. But I'd like to get rid of the error.

My minimal example for a simple test class is:

Code: Select all

\NeedsTeXFormat{LaTeX2e}[1995/12/01]
\ProvidesClass{testclass}[2012/10/24 v0.10 Test class]
\LoadClass[twocolumn]{article}
\RequirePackage{lipsum}

\AtBeginDocument{
  \twocolumn[
    {\lipsum[1]}
  ]
}
The main file simply is:

Code: Select all

\documentclass{testclass}

\begin{document}
\lipsum[2-8]
\end{document}
(lipsum is not an issue. The error occurs with other dummy text as well.)

I'd really wish to understand why this happens. Thanks a lot in advance!

Falko
Last edited by falko on Thu Oct 25, 2012 8:42 am, edited 1 time in total.

Recommended reading 2024:

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

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

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Switch to one column in two-columned document

Post by cgnieder »

Hi Falko,

Welcome to the LaTeX community!

It's unclear to me why you want to call \twoclumn if you already specified twocolumn as option to the article class? It works without:

Code: Select all

\RequirePackage{filecontents}
\begin{filecontents}{testclass.cls}
\NeedsTeXFormat{LaTeX2e}[1995/12/01]
\ProvidesClass{testclass}[2012/10/24 v0.10 Test class]
\LoadClass[twocolumn]{article}

\endinput
\end{filecontents}

\documentclass{testclass}
\usepackage{lipsum}
\begin{document}
\lipsum
\end{document}
Regards
site moderator & package author
falko
Posts: 4
Joined: Wed Oct 24, 2012 1:08 pm

Switch to one column in two-columned document

Post by falko »

When running my sample code I get - as intended - one paragraph across both columns (the \lipsum[1] part) and all following paragraphs in two separate columns (\lipsum[2-8]). What bothers me is the somehow unnecessary "missing \begin{document}" error.

Your example prints everything in two columns.

Falko
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Switch to one column in two-columned document

Post by cgnieder »

How about this:

Code: Select all

\RequirePackage{filecontents}
\begin{filecontents}{testclass.cls}
\NeedsTeXFormat{LaTeX2e}[1995/12/01]
\ProvidesClass{testclass}[2012/10/24 v0.10 Test class]
\LoadClass{article}
\RequirePackage{multicol}
\AtBeginDocument{\lipsum[1]\begin{multicols}{2}}
\AtEndDocument{\end{multicols}}

\endinput
\end{filecontents}

\documentclass{testclass}
\usepackage{lipsum}
\begin{document}
\lipsum
\end{document}
Regards
site moderator & package author
falko
Posts: 4
Joined: Wed Oct 24, 2012 1:08 pm

Switch to one column in two-columned document

Post by falko »

This is indeed a solution for some standard text.
Unfortunately it does not work with floating figures etc. that might occur in a normal two-column document.

Adding a figure within the document environment yields:
Package multicol Warning: Floats and marginpars not allowed inside `multicols' environment!.

Falko
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Switch to one column in two-columned document

Post by cgnieder »

Well, you could use the pagewide variants as described in the multicol manual...

But anyway. The reason why your initial code fails is the optional argument of \twocolumn. If it is given \@topnewpage is called. The first thing this command does is issue the “Missing \begin{document}” error if it isn't used after \begin{document}. This is caused by the macro \@nodocument. You could do the following: patch \@topnewpage to not raise the error. For this you could use etoolbox's \patchcmd:

Code: Select all

\RequirePackage{filecontents}
\begin{filecontents}{testclass.cls}
\NeedsTeXFormat{LaTeX2e}[1995/12/01]
\ProvidesClass{testclass}[2012/10/24 v0.10 Test class]
\LoadClass{article}
\RequirePackage{lipsum}
\RequirePackage{etoolbox}
\patchcmd[\long]%   prefix
  {\@topnewpage}%   cmd
  {\@nodocument}{}% search & replace
  {}{}%             success & failure
\AtBeginDocument{\twocolumn[{\lipsum*[1]}]}

\endinput
\end{filecontents}

\documentclass{testclass}

\begin{document}
\lipsum
\end{document}
Regards
site moderator & package author
falko
Posts: 4
Joined: Wed Oct 24, 2012 1:08 pm

Switch to one column in two-columned document

Post by falko »

Great! That's what I was looking for. Thanks, Clemens!

I just wonder why there was no preceding \begin{document} when parsing the \AtBeginDocument part...

Anyway. The class is working well now. :)

Falko
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Switch to one column in two-columned document

Post by cgnieder »

The command \document that is called by \begin{document} calls \@begindocumenthook which executes everything that's stored with \AtBeginDocument. This happens before the following line in \document is expanded:

Code: Select all

\global\let \@nodocument \relax
which serves the purpose to disable the error message.

Regards
site moderator & package author
Post Reply