# Independent sectioning commands for minipage

My document has several minipage-environments that need an inner sectioning with numeration independent of the "normal" sectioning of the document. Until now I have used a work-around and simply hard-coded the headers as paragraphs and applied the numbering and formatting by hand, e.g.

\begin{minipage}
\begin{center}Title\end{center}

\textbf{1 First section}

Some Text

\textit{1.1 First subsection}

Some more Text

\textit{1.2 Second subsection}

\begin{enumerate}
\item Foo
\item Bar
\end{enumerate}

\textbf{2 Second section}

Even more Text
\end{minipage}


However, this caused some blemishes like too narrow vertical spacing between headers and normal paragraphs in comparison to the vertical space between a "header" followed immediately by a list-environment. Moreover, I would like to have no indention of the first paragraph after a section. To make it short: I would like to have all this nice spacing correction "magic" that is applied by proper sectioning commands.

I would like to define three special minipage (mp) sectioning macros:

• \mptitle: First command in minipage to print a centered title
• \mpsection: 1st-level sectioning
• \mpsubsection: 2nd-level sectioning

with the following properties

• Numbering restarts for each minipage (i.e. similar to footnotes inside minipages)
• Entry in TOC not required and not wanted
• Proper vertical spacing before and after with all these nice spacing corrections, if two sectioning commands immediately follows each other, no indention of following paragraph, corrections for lists, etc.
• Working cross-reference (i.e. using \label) is nice to have, but not yet required

I had a look into source2e.pdf, chap. 61 and tried to figure out what \@startsection, \@sect and friends do and if I possible could re-use them, but I failed. Firstly, I was lost to really understand how these commands work. Secondly, I got the impression that the LEVEL parameter will always interfere with the normal sectioning (I read that is must be unique) and it seems that \@startsection, \@sect always manipulate the TOC.

I would really glad if somebody could help me to define \mptitle, \mpsection and \mpsubsection analogous to what the normal sectioning commands do but omit all the unnecessary code that is required for TOC.

• I am not sure about what kind of "magic space" you need... otherwise that could be done with a couple of counters and specific macros to typeset the section/subsection thingy. – Rmano Jul 5 at 10:06
• @nagmat84 AFAICT, \@startsection doesn't touch the TOC if called with a star following (since this is what \section* and friends do). When \@ifstar is tested at the end of \@startsection's definition, if the result is true, \@ssect{#3}{#4}{#5}{#6} is left in the input stream, which should not alter the TOC (note that it's \@ssect in this case, not \@sect as used for numbered sections). To suppress indentation after a heading made with \@startsection, pass it a negative #4 (beforeskip). – frougon Jul 5 at 11:22
• @frougon The starred version of \@startsection does not help here, because it does not only skip a TOC entry, but does not number the section at all – nagmat84 Jul 5 at 16:17

Based on the code you posted, this implements your items 1, 2 and 3 (item 2, i.e, removing penalty insertions, doesn't seem very important, though).

You may want to consider defining your own environment that wraps minipage and resets the high-level counter (the code given below does that for minipage, as per your request):

\AtBeginEnvironment{yourminipagewrapper}{%
\setcounter{mpsection}{0}%
}


This could be helpful if further customizations are needed in the future. Note that the low-level counter mpsubsection doesn't need to be manually reset, since it is subordinate to the higher-level counter mpsection according to your definition:

\newcounter{mpsubsection}[mpsection]


Also, your minipage-wrapping environment could accept an additional argument used when defining \@currentlabel, otherwise when you see a reference text like « 1.2 », you know it is a section 1.2, but don't know which minipage it belongs to (hyperref knows, though). But this very much depends on how you are going to use all this, so I can only offer simple suggestions like this.

The code:

\documentclass{article}
\usepackage{lipsum}
\usepackage{etoolbox}
\usepackage{hyperref}

\makeatletter

% #1 Name
% #2 Indent
% #3 Beforeskip
% #4 Afterskip
% #5 Style
\newcommand*{\@mpstartsection}[6]{%
\if@noskipsec \leavevmode \fi
\par
\@tempskipa #3\relax
\@afterindenttrue
\ifdim \@tempskipa <\z@
\@tempskipa -\@tempskipa \@afterindentfalse
\fi
\if@nobreak
\everypar{}%
\else
\fi
\refstepcounter{cont@#1}% this counter is never reset, used for hyperref
\stepcounter{#1}% counter used for display purposes
\protected@edef\@currentlabel{% define how refs look
\csname p@#1\endcsname \csname the#1\endcsname
}%
\protected@edef\@svsec{\@seccntformat{#1}\relax}%
\begingroup
#5{\@hangfrom{\hskip #2\relax\@svsec}#6\@@par}%
\endgroup
\par
\vskip #4\relax
% \@afterheading deals with penalities related to page breaking that aren't
% needed inside a minipage, but it also honors the \if@afterindent switch,
% therefore leave it here.
\ignorespaces
}

\newcounter{mpsection}
\newcounter{mpsubsection}[mpsection]

\newcounter{cont@mpsection}
\newcounter{cont@mpsubsection}

\renewcommand\thempsection{\@arabic\c@mpsection}
\renewcommand\thempsubsection{\thempsection.\@arabic\c@mpsubsection}

\AtBeginEnvironment{minipage}{%
\setcounter{mpsection}{0}%
}

\newcommand\mpsection{\@mpstartsection{mpsection}{\z@}{-3.5ex \@plus -1ex \@minus -.2ex}{2.3ex \@plus.2ex}{\normalfont\normalsize\bfseries}}
\newcommand\mpsubsection{\@mpstartsection{mpsubsection}{\z@}{-3.25ex \@plus -1ex \@minus -.2ex}{1.5ex \@plus .2ex}{\normalfont\normalsize\itshape}}

\makeatother

\setlength{\parindent}{0pt}

\begin{document}
\begin{minipage}{\linewidth}
\mpsection{Foo bar section}
\label{sec-foobar}%

\lipsum[1]. See~\ref{sec-subbleh}.

\mpsection{The baz and the quux}
\label{sec-baz-quux}%

\lipsum[2]. See~\ref{sec-foobar} and~\ref{sec-other-subbleh}.
\end{minipage}

\bigskip
\begin{minipage}[t]{0.4\linewidth}
\mpsection{Bleh}
\label{sec-bleh}%

\lipsum[1][2-3]. See~\ref{sec-baz-quux}.

\mpsubsection{Sub-bleh}
\label{sec-subbleh}%

\lipsum[2][4-5]. See~\ref{sec-subouch}.

\mpsubsection{Other sub-bleh}
\label{sec-other-subbleh}%

Bla bla \lipsum[12].

\end{minipage}\hfill
\begin{minipage}[t]{0.4\linewidth}
\mpsection{Ouch!}
\label{sec-ouch}%

\lipsum[1][6-7]

\mpsubsection{Sub-ouch!}
\label{sec-subouch}%

\lipsum[2][8]. See~\ref{sec-ouch} and~\ref{sec-baz-quux}.

\mpsection{Pouet}
\label{sec-pouet}%

\lipsum[3][1-2].

\mpsubsection{Sub-pouet}

\lipsum[3][4-5]
\end{minipage}

\end{document}


On page 1:

On page 2:

• It's excellent the way it is. It is OK, that the printable versions of references to minipage sections are ambiguous. Every minipage is inside a floating figure environment. If I refer to a section inside a minipage, I only do so in combination with a reference to the figure environment. I.e. I always write something like "see \ref{sec:mp-section} in \ref{fig:minipage}". – nagmat84 Jul 7 at 15:27

That is what I have already achieved. The solution is based on what the original \@startsection and \@sect and \@xsect-macros do. I omitted some if-then-else branches that are not required for minipages, throw out the TOC-code and combined the remaining code into one macro.

\makeatletter

% #1 Name
% #2 Indent
% #3 Beforeskip
% #4 Afterskip
% #5 Style
\def\@mpstartsection#1#2#3#4#5#6{%
\if@noskipsec \leavevmode \fi
\par
\@tempskipa #3\relax
\@afterindenttrue
\ifdim \@tempskipa <\z@
\@tempskipa -\@tempskipa \@afterindentfalse
\fi
\if@nobreak
\everypar{}%
\else
\fi
\refstepcounter{#1}%
\protected@edef\@svsec{\@seccntformat{#1}\relax}%
\begingroup
#5{\@hangfrom{\hskip #2\relax\@svsec}\interlinepenalty \@M #6\@@par}
\endgroup
\par\nobreak
\vskip #4\relax
\ignorespaces
}

\newcounter{mpsection}
\newcounter{mpsubsection}[mpsection]

\renewcommand\thempsection{\@arabic\c@mpsection}
\renewcommand\thempsubsection{\thempsection.\@arabic\c@mpsubsection}

\newcommand\mpsection{\@mpstartsection{mpsection}{\z@}{-3.5ex \@plus -1ex \@minus -.2ex}{2.3ex \@plus.2ex}{\normalfont\normalsize\bfseries}}
\newcommand\mpsubsection{\@mpstartsection{mpsubsection}{\z@}{-3.25ex \@plus -1ex \@minus -.2ex}{1.5ex \@plus .2ex}{\normalfont\normalsize\itshape}}

\makeatother


Required improvements

1. Reset the mpsection-counter for each minipage.
2. The code above can probably simplified even more and some penalties are superfluous, because page breaks within minipages are impossible.
3. If 1. was implemented, I would receive a lot of "duplicate destination" errors together with hyperef and cross-references, because there is a section n in each minipage. Somehow the \mptitle command (not implemented yet) need to implement an "invisible" counter that makes the sections unique.