1

I am using the \aligned environment within an \enumerate setup for formatting my assignment submissions. I have tried using the \align*, \flalign*, and \alignat environments but they didn't behave well in my experiments. I have created a new environ that defaults \item content to math mode (appropriate for my use case). However, when I need to enter longer bits of text, it overflows the right margin.

\text overflowing the right margin in aligned environment:

Image of text overflowing the right margin

To address this, I created a \mathbox command that is intended to create a \parbox extending exactly to the right margin from where it is placed. Unfortunately, the calculation seems to be off and the \parbox either overflows or doesn't reach the margin.

\parbox not reaching the right margin:

Image of \parbox not reaching the right margin

\parbox not overflowing the right margin:

Image of \parbox overflowing the right margin

These are the relevant parts of my code:

\documentclass[10pt]{article}
% ...
\usepackage{geometry}
\geometry{a4paper}

%%% packages - not sure which ones were necessary so I just included them all
\usepackage{graphicx,booktabs,array,paralist,verbatim,subfig,sectsty, makecell,enumitem,titlesec,lmodern,slantsc,listings,setspace,amsmath, environ,showframe,nccmath,xifthen,adjustbox}
\usepackage[fleqn]{mathtools}
% ...
\subsectionfont{\fontsize{11}{13.75}\sffamily\mdseries\scshape}
\renewcommand{\labelenumi}{\textbf{Question \arabic*.}}
\renewcommand{\labelenumii}{(\arabic{enumi}.\arabic{enumii})}
\setlist[enumerate]{align=left, listparindent=2em, parsep=0pt}
% ...
\newenvironment{answergroup}{\begin{enumerate}[parsep=10pt]\begingroup\everymath{\displaystyle}
\setstretch {1.3}}{\endgroup\end{enumerate}}
\newenvironment{answerbody}{\begingroup\everymath{\displaystyle}
\setstretch {1.3}}{\endgroup}
\newcommand{\nextline}{\end{aligned}\\\begin{aligned}[t]&\\&}
\NewEnviron{aligneditem}[1][]{
    \begingroup
    %\allowdisplaybreaks - does nothing in aligned mode
    \setlength{\abovedisplayskip}{0pt}
    \setlength{\belowdisplayskip}{0pt}
    \setlength{\abovedisplayshortskip}{0pt}
    \setlength{\belowdisplayshortskip}{0pt}
    \ifthenelse{\isempty{#1}}
    {\item}
    {\item[#1]}
    $\begin{aligned}[t]
    &\BODY
    \end{aligned}$
    \endgroup
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% THIS ↓ \dimexpr needs to be fixed somehow %%%%%%%%%%%%%%%
\newcommand{\mathbox}[1]{\boxed{\parbox[t]{\dimexpr\linewidth-\listparindent-\itemindent}{#1}}}%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\section{Basic Distributions}
\subsection{Binomial Distributions}
\begin{enumerate}
\setcounter{enumi}{3}
\begin{answerbody}
\begin{aligneditem}
\mathbox{
The binomial distribution arises when a random variable is determined to be the number of successes in a series of $n$ independent random trials. In these trials the probability of success is $p$ for each trial, and there are only two possible outcomes: success or failure.
}
\end{aligneditem}
\end{answerbody}
\begin{answerbody}
\begin{aligneditem}
\end{aligneditem}
\end{answerbody}
\begin{answergroup}
\begin{aligneditem}[\unskip]
\mu \geq \frac{5 * 0 + 30 * 1 + 56 * 2 + 15 * 3 + 10 * 4 + 7 * 5}{123} = \frac{262}{123} \\&
\implies \text{Possibly }X \sim \text{Po}(\lambda)\text{ where }\lambda \geq \frac{262}{123}\\&
\mathbox{It appears that sampled stroke patients make at least 2.13 errors on average on this psychometric test.}\\&
\displaystyle\sigma^{2} \geq \sum_{x}{\frac{(x-\mu)^{2}}{N}} \\&
= \frac{1}{123}\left(\begin{multlined}5\left(0 - \frac{262}{123}\right)^{2} + 30\left(1 - \frac{262}{123}\right)^{2} + 56\left(2 - \frac{262}{123}\right)^{2}\\
+ 15\left(3 - \frac{262}{123}\right)^{2} + 10\left(4 - \frac{262}{123}\right)^{2} + 10\left(5 - \frac{262}{123}\right)^{2}\end{multlined}\right)\\&
% ...
\end{aligneditem}
\end{answergroup}
\end{enumerate}
\end{document}

How should I properly calculate the remaining space to the right margin for \parbox?

3
  • 3
    Welcome to TeX.SE!
    – Mensch
    Sep 7, 2021 at 20:53
  • 1
    your \mathbox is a framed text (not math) \parbox so I don't understand the name but something like \newcommand{\mathbox}[1]{\fbox{\parbox[t]{\dimexpr\linewidth-\labelwidth-\labelsep-\listparindent-\itemindent-2\fboxsep-2\fboxrule}{#1}}}% Sep 7, 2021 at 21:22
  • I agree that the name is somewhat misleading, but it can be changed. Your suggestion seem to leave the box on the left of the margin, but leaves a gap between the right side of the box and the margin. Is there a way that makes this gap as small as possible?
    – Avi
    Sep 7, 2021 at 21:29

1 Answer 1

0

I solved this problem by changing the width of the \parbox to \dimexpr\textwidth-\leftmargin-\listparindent+\parskip-\labelsep-\listparindent. I'm not quite sure why this works, but it gets reasonably close to the margin without overflowing, for my use cases. The code after modification is:

\documentclass[10pt]{article}
% ...
\usepackage{geometry}
\geometry{a4paper}

%%% packages - not sure which ones were necessary so I just included them all
\usepackage{graphicx,booktabs,array,paralist,verbatim,subfig,sectsty, makecell,enumitem,titlesec,lmodern,slantsc,listings,setspace,amsmath, environ,showframe,nccmath,xifthen}
\usepackage[fleqn]{mathtools}
% ...
\subsectionfont{\fontsize{11}{13.75}\sffamily\mdseries\scshape}
\renewcommand{\labelenumi}{\textbf{Question \arabic*.}}
\renewcommand{\labelenumii}{(\arabic{enumi}.\arabic{enumii})}
\setlist[enumerate]{align=left, listparindent=2em, parsep=0pt}
% ...
\newenvironment{answergroup}{\begin{enumerate}[parsep=10pt]\begingroup\everymath{\displaystyle}
\setstretch {1.3}}{\endgroup\end{enumerate}}
\newenvironment{answerbody}{\begingroup\everymath{\displaystyle}
\setstretch {1.3}}{\endgroup}
\newcommand{\nextline}{\end{aligned}\\\begin{aligned}[t]&\\&}
\newcommand{\textbox}[1]{\boxed{\parbox[t]{\dimexpr\textwidth-\leftmargin-\listparindent+\parskip-\labelsep-\listparindent}{#1}}}
\NewEnviron{aligneditem}[1][]{
    \begingroup
    %\allowdisplaybreaks - does nothing in aligned mode
    \setlength{\abovedisplayskip}{0pt}
    \setlength{\belowdisplayskip}{0pt}
    \setlength{\abovedisplayshortskip}{0pt}
    \setlength{\belowdisplayshortskip}{0pt}
    \ifthenelse{\isempty{#1}}
    {\item}
    {\item[#1]}
    $\begin{aligned}[t]
    &\BODY
    \end{aligned}$
    \endgroup
}
\begin{document}
\section{Basic Distributions}
\subsection{Binomial Distributions}
\begin{enumerate}
\setcounter{enumi}{3}
\begin{answerbody}
\begin{aligneditem}
\textbox{
The binomial distribution arises when a random variable is determined to be the number of successes in a series of $n$ independent random trials. In these trials the probability of success is $p$ for each trial, and there are only two possible outcomes: success or failure.
}
\end{aligneditem}
\end{answerbody}
\begin{answerbody}
\begin{aligneditem}
\end{aligneditem}
\end{answerbody}
\begin{answergroup}
\begin{aligneditem}[\unskip]
\mu \geq \frac{5 * 0 + 30 * 1 + 56 * 2 + 15 * 3 + 10 * 4 + 7 * 5}{123} = \frac{262}{123} \\&
\implies \text{Possibly }X \sim \text{Po}(\lambda)\text{ where }\lambda \geq \frac{262}{123}\\&
\textbox{It appears that sampled stroke patients make at least 2.13 errors on average on this psychometric test.}\\&
\displaystyle\sigma^{2} \geq \sum_{x}{\frac{(x-\mu)^{2}}{N}} \\&
= \frac{1}{123}\left(\begin{multlined}5\left(0 - \frac{262}{123}\right)^{2} + 30\left(1 - \frac{262}{123}\right)^{2} + 56\left(2 - \frac{262}{123}\right)^{2}\\
+ 15\left(3 - \frac{262}{123}\right)^{2} + 10\left(4 - \frac{262}{123}\right)^{2} + 10\left(5 - \frac{262}{123}\right)^{2}\end{multlined}\right)\\&
% ...
\end{aligneditem}
\end{answergroup}
\end{enumerate}
\end{document}

It produces this output: Final output with proper alignment

You must log in to answer this question.

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