3

Background

I've created a macro with name \an in order to write single line and multiline annotations inside the alignat environment (see example below)

\documentclass{article}

\usepackage{amsmath, stackengine, lipsum}

\setstackEOL{\\}

\newcommand\an[1]{%
\scantokens{&\hspace{10px}& {\tiny \Longstack[l]{(#1)}}}
}

\begin{document}
\begin{alignat*}{2}
x &= 0 \an{a}
\\ &= 0 + 0 + 0 \an{\lipsum[1][1] \\ \lipsum[2][1]}
\end{alignat*}
\end{document}[

a

The problem

I sometimes need to write long pieces of text inside those annotations which makes me insert a newline whenever it reaches the end of the page. Otherwise, the following would occur

a

The current workaround is to manually insert a newline character in those places where I want a breakline, but that's a repetitive task that I think it is possible to automate by using the value of the \textwidth variable.

The question

So, my question is: How can I make the \an macro insert a newline character whenever the argument passed reaches the \textwidth value?

gfe
  • 415
  • It is difficult because you are in an amsmath environment, otherwise you could use the answers to this question. –  May 01 '20 at 05:08
  • I suspect that, since amsmath displayed equations do a pre-typesetting measurement phase, it might be possible to find the available width, and typeset the text in an appropriate \parbox on the second pass. – Donald Arseneau May 01 '20 at 05:54

1 Answers1

2

This works. However, it is necessary to compile the document twice to make linebreaks to be set correctly.

\documentclass{article}

\usepackage{amsmath, linegoal, lipsum}
\usepackage[showframe, margin=5cm]{geometry}

\newcommand\an[1]{&\hspace{10px}&\parbox[t]{\linegoal}{\tiny (#1)}}

\begin{document}

\lipsum[1][1-4]

\begin{alignat*}{2}
x &= 0 \an{a} 
\\ &= 0 + 0 \an{\lipsum[1][1-2]}
\\ &= 0 + 0 + 0 \an{\lipsum[1][1-3]}
\\ &= 0 + 0 + 0 + 0 \an{\lipsum[1][1-4]}
\end{alignat*}
\end{document}

enter image description here

gfe
  • 415
Fran
  • 80,769