# In horizontally aligned minipages, how can I force the last minipage width to extend to the right margin

I need to use minipage environment to give side by side descriptions of equations. The minipages themselves must be separated by equal distance, which is in this case a quad. However, it would not be clear in such case what the last minipage would be. I want the width of the last minipage to be determined automatically so that its contents will stretch all the way until the allowable margin for text.

A sample pics are shown here. The original output:

Desired output:

The output above is obtained by fine-tuning the width of the last minipage, but if the margins were adjusted here, the minipage will either be too big to fit with other minipages or it will not extend until it reaches the margins.For this case, I can get the desired output with 0.43\textwidth, but I want LaTeX to automatically detect and specify the width of the final minipage.

I know that this issue was addressed here: minipage that fills space to right margin by using the command \dimexpr \textwidth-\wd0-\columnsep to specify text width of the last minipage, but the solution does not seem to work for my particular content. The output after including this command:

My source code:

\documentclass[]{book}
\usepackage{amsmath}
\usepackage{mathtools}

\usepackage[showframe]{geometry}

\begin{document}

\noindent You can use multiple \texttt{minipage} environments for side-by-side descriptions:
\begin{center}
\begin{minipage}{0.25\textwidth}
\vspace{-\baselineskip}
\begin{align}
f_1 &= a + b &\\
f_2 &= a - b &
\end{align}
\end{minipage}
\quad
$\begin{array}{c @{\medspace}c @{\medspace}l} a &= &\text{Some one line text}\\ b &= &\text{Some two line text}\\ & &\text{The second line} \end{array}$
\quad
\begin{minipage}{0.3\textwidth}
This is detailed description of the equation. Note how the two equations are both centered with respect to what is mentioned here. Such code could be so useful to give comments for tutorials and the like.
\end{minipage}

\end{center}

\end{document}

• You can replace one (or both) \quad by \hfill... – Phelype Oleinik Apr 16 '18 at 22:30
• your code gives desired output (the second image). what is the problem? – Zarko Apr 16 '18 at 22:31
• The problem is that I had to specify exactly how much the textwidth would be, but if I changed the margin even slightly, the situation changes. – Al-Motasem Aldaoudeyeh Apr 16 '18 at 22:32
• @Phelype Oleinik I want the spacing between minipages to stay constant at \quad and the last minipage must stretch accordingly. With \hfill, the distance will adjust and can be different from \quad – Al-Motasem Aldaoudeyeh Apr 16 '18 at 22:34
• @Al-MotasemAldaoudeyeh Oh, sorry, I misunderstood your problem. GuM's solution is the way to go :) – Phelype Oleinik Apr 16 '18 at 23:02

## 4 Answers

You can make tabularx calculate the last width automatically for you.

\documentclass[]{book}
\usepackage{amsmath}
\usepackage{mathtools,tabularx}
\usepackage[showframe]{geometry}

\begin{document}

\noindent%
\begin{tabularx}{\linewidth}{@{} *2{>{\hsize=.5\hsize}X} X@{}}
\begin{align}
f_1 = a + b \\
f_2 = a - b
\end{align}
&
\begin{equation*}
\begin{aligned}
a ={} &\text{Some one line text}\\
b ={} &\text{Some two line text}\\
&\text{The second line}
\end{aligned}
\end{equation*}
&
This is detailed description of the equation. Note how the two equations are both centered with respect to what is mentioned here. Such code could be so useful to give comments for tutorials and the like.
\end{tabularx}

\end{document}


Two variants with tabularx, redefining the X column type as m{some length}:

\documentclass{book}
\usepackage[showframe]{geometry}
\usepackage{mathtools}
\usepackage{tabularx}
\renewcommand{\tabularxcolumn}[1]{>{\arraybackslash}m{#1}}

\begin{document}

\noindent \begin{tabularx}{\linewidth}{@{}ccX@{}}
\begin{aligned} f_1 &= a + b \\ f_2 &= a - b \end{aligned}
&
{$\begin{array}{c @{\medspace}c @{\medspace}l} a &= &\text{Some one line text}\\ b &= &\text{Some two line text}\\ & &\text{The second line} \end{array}$}
&
This is a detailed description of the equation. Note how the two equations are both centered with respect to what is mentioned here. Such code could be so useful to give comments for tutorials and the like.\\
\end{tabularx}
\vspace{1cm}

\noindent \begin{tabularx}{\linewidth}{@{} >{\centering$}m{0.25\textwidth}<{$}cX@{}}
\begin{aligned}
f_1 &= a + b \\
f_2 &= a - b
\end{aligned}
&
{$\begin{array}{c @{\medspace}c @{\medspace}l} a &= &\text{Some one line text}\\ b &= &\text{Some two line text}\\ & &\text{The second line} \end{array}$}
&
This is a detailed description of the equation. Note how the two equations are both centered with respect to what is mentioned here. Such code could be so useful to give comments for tutorials and the like.\\
\end{tabularx}

\end{document}


As all the other answers use tabularx, this is another way:

The problem with minipages or parboxes is only that if you have three boxes (said A,B,C) with two \quad (1em) spaces, then the width of C must be the text width (e.g., \linewidth) minus the width of A+B+2em, and you can do this with \dimexpr.

One more way could be use the grid-system package, where you simply assign widths to the three columns using arbitrary integers, taking into account the sum of the three integers is the text width (minus the column separations ) no matter if the sum is 3, 17 or 30.

The MWE show both approaches:

\documentclass{book}
\usepackage{amsmath}
\usepackage{grid-system}
\usepackage{geometry}
\newlength\boxa
\newlength\boxb
\newlength\boxc
\parindent0pt
\parskip1em

\begin{document}

\hrulefill With \verb|\dimexpr|  \hrulefill

\setlength\boxa{.2\linewidth}
\setlength\boxb{.3\linewidth}
\setlength\boxc{\dimexpr\linewidth-\boxa-\boxb-2em}

\parbox[t]{\boxa}{\begin{align}
f_1 &= a + b &\\
f_2 &= a - b &
\end{align}}\quad%
\parbox[t]{\boxb}{$\begin{array}{c @{\medspace}c @{\medspace}l} a &= &\text{Some one line text}\\ b &= &\text{Some two line text}\\ & &\text{The second line} \end{array}$}\quad%
\parbox[t]{\boxc}{This is detailed description of the equation. Note how the two equations are both centered with respect to what is mentioned here. Such code could be so useful to give comments for tutorials and the like.}

\setlength\boxa{.3\linewidth}
\setlength\boxb{.4\linewidth}
\setlength\boxc{\dimexpr\linewidth-\boxa-\boxb-2em}

\parbox[t]{\boxa}{\begin{align}
f_1 &= a + b &\\
f_2 &= a - b &
\end{align}}\quad%
\parbox[t]{\boxb}{$\begin{array}{c @{\medspace}c @{\medspace}l} a &= &\text{Some one line text}\\ b &= &\text{Some two line text}\\ & &\text{The second line} \end{array}$}\quad%
\parbox[t]{\boxc}{This is detailed description of the equation. Note how the two equations are both centered with respect to what is mentioned here. Such code could be so useful to give comments for tutorials and the like.}

\hrulefill With \verb|grid-system|  \hrulefill

\begin{Row}[cellsep=1em]%
\begin{Cell}{4}
\begin{align}
f_1 &= a + b &\\
f_2 &= a - b &
\end{align}
\end{Cell}
\begin{Cell}{4}
$\begin{array}{c @{\medspace}c @{\medspace}l} a &= &\text{Some one line text}\\ b &= &\text{Some two line text}\\ & &\text{The second line} \end{array}$
\end{Cell}
\begin{Cell}{7}
This is detailed description of the equation. Note how the two equations are both centered with respect to what is mentioned here. Such code could be so useful to give comments for tutorials and the like.
\end{Cell}
\end{Row}

\begin{Row}[cellsep=1em]%
\begin{Cell}{1}
\begin{align}
f_1 &= a + b &\\
f_2 &= a - b &
\end{align}
\end{Cell}
\begin{Cell}{1}
$\begin{array}{c @{\medspace}c @{\medspace}l} a &= &\text{Some one line text}\\ b &= &\text{Some two line text}\\ & &\text{The second line} \end{array}$
\end{Cell}
\begin{Cell}{1}
This is detailed description of the equation. Note how the two equations are both centered with respect to what is mentioned here. Such code could be so useful to give comments for tutorials and the like.
\end{Cell}
\end{Row}

\end{document}


Why do you say that the code in the other answer doesn’t work?

\documentclass[a4paper]{book}
\usepackage{mathtools} % automatically loads "amsmath" too

\begin{document}

\noindent
You can use multiple \texttt{minipage} environments for side-by-side
descriptions (some more text to show where the right margin is):

\begin{center}
\begin{minipage}{.25\linewidth}
\vspace{-\baselineskip}
\begin{align}
f_1 &= a + b &\\
f_2 &= a - b &
\end{align}
\end{minipage}%
\quad
\sbox{0}{%
$\begin{array}{c @{\medspace}c @{\medspace}l} a &= &\text{Some one line text}\\ b &= &\text{Some two line text}\\ & &\text{The second line} \end{array}$%
}%
\usebox{0}%
\quad
\begin{minipage}{\dimexpr .75\linewidth-2em-\wd0 \relax}
This is detailed description of the equation.  Note how the two
equations are both centered with respect to what is mentioned here.
Such code could be so useful to give comments for tutorials and the
like.
\end{minipage}%
\end{center}

\end{document}


Beware: do not remove any % character!

Here is the output:

# Addition

I too, like @AboAmmar, had originally thought of using the tabularx environment. However, let's honor the OP desire that the middle column has its natural width:

\documentclass[a4paper]{book}
\usepackage{mathtools} % automatically loads "amsmath" too
\usepackage{tabularx}

\begin{document}

\noindent
You can use multiple \texttt{minipage} environments for side-by-side
descriptions (some more text to show where the right margin is):
%
\begin{center} % The main purpose of this environment is to limit the scope of
% the following "\renewcommand"; additionally, it supplies
% appropriate vertical space.
\renewcommand*{\tabularxcolumn}[1]{m{#1}}
\noindent
\begin{tabularx}{\linewidth}{@{}m{.25\linewidth}@{\quad}c@{\quad}X@{}}
\begin{align}
f_1 = a + b \\
f_2 = a - b
\end{align}
&
\displaystyle \begin{aligned} a ={} &\text{Some one line text}\\ b ={} &\text{Some two line text}\\ &\text{The second line} \end{aligned}
&
This is detailed description of the equation. Note how the two equations are both centered with respect to what is mentioned here. Such code could be so useful to give comments for tutorials and the like.
\end{tabularx}
\end{center}
%
Some text below the environment.

\end{document}


Here is the corresponding output: