5
\section{Introduction}

I want to write a macro to that makes the above behave the same as:

\usepackage{multicol}
\begin{multicols}{2}[\section{Introduction}]

In praise of code convolution avoidance: The shortest and simplest answer(Most adhering to the KISS philosophy) will be the accepted answer. DON'T use a library if you don't NEED to.

2
  • 2
    Your question is not clear. You want to write \section{Introduction} and have it be the same as \begin{multicols}{2}[\section{Introduction}]? Your directives about what answers are acceptable are ill-advised and frankly, kinda rude.
    – TH.
    Commented Oct 10, 2010 at 5:15
  • 1
    You should make it clear whether or not the matching \end{multicols} should also be handled automatically, because taking your request literally, it shouldn't.
    – frabjous
    Commented Oct 10, 2010 at 5:30

2 Answers 2

6

Here's the best I could come up with. First, I define \oldsection as the old \section macro, then use it to define a new \section command which first ends the previous multicolumn environment, unless it's the first section, in which case it does nothing, and then starts a new multicolumn environment with a section heading as defined by old section.

I also have to redefine \enddocument to end the final multicolumn.

This is not a very robust or advisable solution, but may work in a pinch.

\usepackage{multicol}
\usepackage{ifthen}
\let\oldsection\section
\renewcommand*{\section}[1]{%
    \ifthenelse{\thesection = 0}{}{\end{multicols}}%
    \begin{multicols}{2}[\oldsection{#1}]}
\let\oldend\enddocument
\renewcommand*{\enddocument}{\end{multicols}\oldend}

To used as follows:

\documentclass{article}
\usepackage{lipsum}
\usepackage{multicol}
\usepackage{ifthen}
\let\oldsection\section
\renewcommand*{\section}[1]{%
    \ifthenelse{\thesection = 0}{}{\end{multicols}}%
    \begin{multicols}{2}[\oldsection{#1}]}
\let\oldend\enddocument
\renewcommand*{\enddocument}{\end{multicols}\oldend}
\begin{document}
        \section{My first section}
        \lipsum[1-2]
        \section{Another section}
        \lipsum[3-4]
\end{document}
2
  • Worked almost like a charm!! If I could just figure out how to make those foot notes appear pretty and above sections following sections(on the same page); I would say this is worth adding to the multicol package as an option. But that would be another question.
    – GlassGhost
    Commented Oct 11, 2010 at 1:45
  • This seems to break if you add \tableofcontents... also, I changed the ifthen to \ifthenelse{\equal{\@currenvir}{multicols}}{\end{multicols}}{} to specifically check if you're in a multicols before issuing \end, otherwise I got errors on a large document. Still working on the \tableofcontents issue though...
    – glallen
    Commented Sep 19, 2012 at 17:28
1

I don't really understand your question, but this might do what you want:

\newenvironment{xmulticols}[1]{\section{#1} \begin{multicols}{2}}{\end{multicols}}

This would only work if you only ever wanted 2 column layout, but is easily fixable to more columns.

I'd also recommend you also change how the section headings look (maybe make them centered?) to make clear that the section heading "covers" both columns...

3
  • 1
    No, I think he wants to write only \section{blah blah} and have it behave this way.
    – frabjous
    Commented Oct 11, 2010 at 0:50
  • Ah I see. I didn't think of that. I guess because I don't like the idea of having environments begin and end without the \begin and \end markers visible. I'm not sure it's a good idea...
    – Seamus
    Commented Oct 11, 2010 at 10:25
  • Agreed. The whole thing makes me nervous. But I still, I like a challenge.
    – frabjous
    Commented Oct 11, 2010 at 15:42

You must log in to answer this question.

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