Tx · LaTeX & TikZ

How to Put Two Minipages Inside a LaTeX Command

Published By the 9to5Science editorial team

At a glance

  1. 1. Define two arguments

    Give the command one argument for each minipage’s contents.

  2. 2. Choose flexible widths

    Use widths that leave room for the horizontal space between the minipages.

  3. 3. Align and separate

    Use top alignment, horizontal fill, and paragraph boundaries to place the boxes cleanly.

  4. 4. Use the command

    Pass the left and right contents in braces when you call the command.

You can put two minipage environments inside a LaTeX command by giving the command two arguments: one for each minipage’s contents. For side-by-side boxes, make each box a little narrower than half the available width and place \hfill between them.

Define a command with two minipages

Here is a reusable command for side-by-side minipages:

\newcommand{\TwoMinipages}[2]{%
  \par\noindent
  \begin{minipage}[t]{.48\linewidth}
    \vspace{0pt}%
    #1
  \end{minipage}\hfill
  \begin{minipage}[t]{.48\linewidth}
    \vspace{0pt}%
    #2
  \end{minipage}%
  \par
}

The #1 and #2 markers refer to the command’s first and second arguments. When you call \TwoMinipages, LaTeX places the first argument inside the first minipage and the second inside the other.

The width .48\linewidth leaves a small amount of spare space for \hfill to stretch. \linewidth means the width currently available for a line of text, so the command can work inside a list or another narrowed area. By contrast, \textwidth refers to the main text block’s width and may be too wide in those contexts.

The [t] option requests top alignment. The \vspace{0pt} at the start of each minipage helps align their content at the top, even when the contents begin differently. The surrounding \par commands put the pair of boxes on its own line.

Complete example

This example needs no extra packages:

\documentclass{article}

\newcommand{\TwoMinipages}[2]{%
  \par\noindent
  \begin{minipage}[t]{.48\linewidth}
    \vspace{0pt}%
    #1
  \end{minipage}\hfill
  \begin{minipage}[t]{.48\linewidth}
    \vspace{0pt}%
    #2
  \end{minipage}%
  \par
}

\begin{document}

\TwoMinipages
  {\textbf{Left box}\par This is the first minipage.}
  {\textbf{Right box}\par This is the second minipage.}

\end{document}

The two brace-delimited groups after \TwoMinipages are its arguments. Paragraph breaks inside either group are allowed, so each minipage can contain more than one paragraph. You can also put lists, headings, or other ordinary LaTeX content inside them.

If the minipages should be stacked

The same idea works for boxes one above the other. Remove \hfill and insert a paragraph break between the minipages. For example, replace the middle of the definition with:

  \end{minipage}\par
  \begin{minipage}[t]{.48\linewidth}

For stacked boxes, you may want each minipage to use \linewidth rather than .48\linewidth, so each occupies the full available width.

Common mistakes

  • Using widths that are too large: Two minipages of width 0.5\linewidth leave no room for a gap. Depending on surrounding spaces, the line may become overfull. Start with widths such as .48\linewidth and adjust if needed.
  • Leaving out the argument markers: Inside a two-argument command definition, #1 and #2 are placeholders. Write #1 and #2 in the definition; put the actual content in braces when calling the command.
  • Expecting an environment to work as a normal argument: Most text and paragraph material can go in a command argument, but verbatim-like content (such as verbatim) cannot generally be passed this way. Use minipages directly for such content, or choose a design that does not capture it as a macro argument.
  • Calling the command in the middle of a paragraph: This definition starts and ends a paragraph. Call it where a separate pair of boxes belongs, rather than expecting it to continue a line of surrounding text.
  • Confusing the alignment option with content alignment: [t] aligns the minipage boxes with each other; it does not center or right-align the text inside them. Use ordinary commands such as \centering within a minipage if you want to align its contents.

For a fixed pair of reusable boxes, a two-argument command is concise. If the contents include verbatim material or need more complex handling, writing the minipages directly is often simpler and more robust.

References

  1. LaTeX2e unofficial reference manual: minipage
  2. LaTeX2e unofficial reference manual: \newcommand and \renewcommand

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.