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 inaligned
environment:
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:
\parbox
not 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
?
\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}}}%