6

I have an enumerate environment inside a minipage (the minipage might not be needed, I only included it here to show the margins). Each item has a paragraph of descriptive text and contains smaller enumerations of items with numbers, that are then summed out for each main item. Finally, the intermediate sums are summed to a total. I would like the intermediate sums to align with the right margin/total. What are my options?

Notes:

  • the amounts are in old Danish money (mark, daler, skilling) of which the summing rules vary through time.
  • the outer enumerate environment will likely span multiple pages.

In the below example, the green margins are OK, the blue ones should be moved along the red arrows to match the green ones: example

MWE:

\documentclass{article}

\usepackage{lipsum}

\begin{document}

\begin{minipage}{.9\textwidth}
\begin{enumerate}

\item \lipsum[1]
    \begin{enumerate}
    \item Something \dotfill 11
    \item Something \dotfill 84 \hfill 95 % "95" needs to break out of minipage and flush to right margin
    \end{enumerate}

\item \lipsum[2]
    \begin{enumerate}
    \item Something \dotfill 4
    \item Something \dotfill 300 \hfill 304 % ditto
    \end{enumerate}

\end{enumerate}
\end{minipage}

\null\hfill Total: 399

\end{document}
  • why do you have the minipage? – David Carlisle May 24 at 15:18
  • To make room so the paragraphs & intermediates dont go where the totals are supposed to be. It can be removed if there is another way. – meide May 24 at 15:24
  • I would have use a tabular or tabularx instead of the nested enumerate. You can even use counters if you have trouble remembering your abcs. – John Kormylo May 24 at 22:15
6

You can put the text in box of fixed size hidden in a box of zero width. Here I have encapsulated this in a \subtotal macro. To make this match you need the total in a corresponding box too. You should calculate the width from the space you know you have left. Note you were missing a \noindent before your minipage.

Sample output

\documentclass{article}

\usepackage{lipsum}

\newlength{\mytotboxwd}
\setlength{\mytotboxwd}{20pt}
\newcommand{\subtotal}[2][\mytotboxwd]{\hbox to 0pt{\hbox to #1{\hss #2}\hss}}
\newcommand{\total}[2][\mytotboxwd]{\hbox to #1{\hss #2}}

\begin{document}

\setlength{\mytotboxwd}{.1\textwidth}
\noindent
\begin{minipage}{.9\textwidth}
\begin{enumerate}

\item \lipsum[1]
    \begin{enumerate}
    \item Something \dotfill 11
    \item Something \dotfill 84\subtotal{95}
    \end{enumerate}

\item \lipsum[2]
    \begin{enumerate}
    \item Something \dotfill 4
    \item Something \dotfill 300\subtotal{304}
    \end{enumerate}

\end{enumerate}
\end{minipage}

\null\hfill\total{Total: 399}

\end{document}
  • Does that line up? The 304 and 399 don't look entirely aligned to me (304 slightly to the right of 399) – meide May 24 at 15:32
  • I have updated to improve this. – Andrew Swann May 24 at 15:37
5

Do you want something like this? It takes two LaTeX runs to synchronize.

\documentclass{article}
\usepackage{microtype} % not essential, but better typesetting

\usepackage{lipsum,showframe}

\newenvironment{totals}
 {%
  \par
  \addtocounter{envtotals}{1}%
  \settowidth{\totalswidth}{\ref{\theenvtotals label}\quad}%
  \setcounter{totals}{0}%
  \begin{list}{}{\leftmargin=0pt\rightmargin=\totalswidth}\item\relax
 }
 {\par
  \addtocounter{totals}{-1}\refstepcounter{totals}\label{\theenvtotals label}%
  \noindent\hfill Total:\subtotalbox{\ref{\theenvtotals label}}%
  \end{list}
 }
\newcommand{\subtotalbox}[1]{\makebox[0pt][l]{\makebox[\totalswidth][r]{#1}}}
\newenvironment{quantities}
  {\begin{enumerate}\setcounter{subtotals}{0}}
  {\unskip\subtotalbox{\thesubtotals}\addtocounter{totals}{\value{subtotals}}\end{enumerate}}
\newcommand{\quantity}[2]{%
  \item #1\dotfill#2%
  \addtocounter{subtotals}{#2}%
}
\newcounter{envtotals}
\newcounter{totals}
\newcounter{subtotals}
\newlength{\totalswidth}

\begin{document}

\begin{totals}
\begin{enumerate}

\item \lipsum[1]
    \begin{quantities}
    \quantity{Something}{11}
    \quantity{Something}{84}
    \end{quantities}

\item \lipsum[2]
    \begin{quantities}
    \quantity{Something}{4}
    \quantity{Something}{300}
    \end{quantities}
\end{enumerate}
\end{totals}

\end{document}

enter image description here

  • Very cool! However, the quantities are in old Danish money (daler, mark, skilling) which makes automatic summing intractable (sometimes a daler is 6 marks, other times 4 marks, etc). – meide May 24 at 18:39
  • @meide There should be a rule… ;-) But it's not difficult to amend the code in order to print the totals as input by hand. – egreg May 24 at 20:19
  • Played around a bit with addition of d.m.s values by converting to/from skilling depending on factors: gist.github.com/mikkelee/aac4ba58e83737f6f0446617870a8141 @egreg – meide May 25 at 2:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.