# How to temporarily escape an 'alignedat' environment nested inside an 'align*' environment?

This is my output:

The problem, of course, is with the misplaced third line. What should happen is this:

• The four left-hand equal signs ought to be aligned;
• additionally minus signs and plus signs from the second and third lines should be aligned, respectively;
• this additional alignment should not affect first or fourth lines.

Here is what I tried (full working example simplified document, so you may copy):

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[LGR,T1]{fontenc}
\usepackage{ae,aecompl}
\usepackage[estonian]{babel}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage[a4paper]{geometry}
\usepackage{enumitem}
\usepackage{mathtools}
\usepackage[pdftex]{graphicx}

\newgeometry{margin=2.5cm}

\DeclareSymbolFont{upgreek}{LGR}{cmr}{m}{n}
\DeclareMathSymbol{\ppi}{\mathord}{upgreek}{p}

\newcommand{\f}[2]{\frac{#1}{#2}}
\newcommand{\Int}[3]{\int\limits_{#1}^{#2}{#3}\, \mathrm{d}x}

\setlength\parindent{0pt}

\begin{document}

\begin{align*}
a_k &= \f{2}{\ppi}\Int{0}{\ppi}{f(x)} = \f{2}{\ppi}\Int{0}{\f{\ppi}{2}}{\left(
\f{\ppi}{2}-x\right)} + \f{2}{\ppi}\Int{\f{\ppi}{2}}{\ppi}{\ppi} = \\
&=\begin{alignedat}[t]{2}
\f{2}{\ppi} \f{\ppi}{2} \left(\f{\ppi}{2}\right)-&
\f{2}{\ppi} \f{1}{2} \left(\f{\ppi}{2}\right)^2 +&
\f{2}{\ppi}\ppi\left(\ppi-\f{\ppi}{2}\right)=\\
&= \f{\ppi}{2} -& \f{\ppi}{4} +& 2\f{\ppi}{2} =
\end{alignedat}\\
&=\f{5}{4}\ppi
\end{align*}

\end{document}


The part that is of interest to this question, of course, is the following subpart of the above:

\begin{document}

\begin{align*}
a_k &= \f{2}{\ppi}\Int{0}{\ppi}{f(x)} = \f{2}{\ppi}\Int{0}{\f{\ppi}{2}}{\left(
\f{\ppi}{2}-x\right)} + \f{2}{\ppi}\Int{\f{\ppi}{2}}{\ppi}{\ppi} = \\
&=\begin{alignedat}[t]{2}
\f{2}{\ppi} \f{\ppi}{2} \left(\f{\ppi}{2}\right)-&
\f{2}{\ppi} \f{1}{2} \left(\f{\ppi}{2}\right)^2 +&
\f{2}{\ppi}\ppi\left(\ppi-\f{\ppi}{2}\right)=\\
&= \f{\ppi}{2} -& \f{\ppi}{4} +& 2\f{\ppi}{2} =
\end{alignedat}\\
&=\f{5}{4}\ppi
\end{align*}

\end{document}


I believe I understand what the problem is. Have a look at the penultimate line inside the alignedat environment. There I have written &=; unfortunately, the line break \\ from the line previous only applies to the alignedat environment. It does not reach the align* environment.

So ideally this \\ should have a dual role, signalling alignedat and align* simultaneously. Simply put, can I do something like this (cf. \insertcommand{}):

\begin{document}

\begin{align*}
a_k &= \f{2}{\ppi}\Int{0}{\ppi}{f(x)} = \f{2}{\ppi}\Int{0}{\f{\ppi}{2}}{\left(
\f{\ppi}{2}-x\right)} + \f{2}{\ppi}\Int{\f{\ppi}{2}}{\ppi}{\ppi} = \\
&=\begin{alignedat}[t]{2}
\f{2}{\ppi} \f{\ppi}{2} \left(\f{\ppi}{2}\right)-&
\f{2}{\ppi} \f{1}{2} \left(\f{\ppi}{2}\right)^2 +&
\f{2}{\ppi}\ppi\left(\ppi-\f{\ppi}{2}\right)=\\
\insertcommand{\\ &=} \f{\ppi}{2} -& \f{\ppi}{4} +& 2\f{\ppi}{2} =
\end{alignedat}\\
&=\f{5}{4}\ppi
\end{align*}

\end{document}


so that the \\ &= is sent to align, and after that, alignedat continues?

I am very much hoping that such a minor tweak suffices; but other answers are welcome, too (if they do not require (much) manual usage of \!-s and so on).

If you want proper (horizontal) alignment across elements within the equation, you can use the measuring capabilities of eqparbox's \eqmakebox[<tag>][<align>]{<stuff>}. All elements with the same <tag> will be put in a box of maximum width (with an optional <align>ment; default is centred):

\documentclass{article}

\usepackage{amsmath,eqparbox}

\newcommand{\f}[2]{\frac{#1}{#2}}
\newcommand{\Int}[3]{\int\limits_{#1}^{#2}{#3}\, \mathrm{d}x}

\let\ppi\pi

\begin{document}

\begin{align*}
a_k &= \f{2}{\ppi} \Int{0}{\ppi}{f(x)}
= \f{2}{\ppi} \Int{0}{\f{\ppi}{2}}{\Bigl( \f{\ppi}{2} - x \Bigr)}
+ \f{2}{\ppi} \Int{\f{\ppi}{2}}{\ppi}{\ppi} \\
&= \eqmakebox[first]{$\displaystyle \underbrace{\f{2}{\ppi} \f{\ppi}{2} \Bigl( \f{\ppi}{2} \Bigr)}$}
- \eqmakebox[second]{$\displaystyle \underbrace{\f{2}{\ppi} \f{1}{2} \Bigl( \f{\ppi}{2} \Bigr)^2}$}
+ \eqmakebox[third]{$\displaystyle \underbrace{\f{2}{\ppi} \ppi \Bigl( \ppi - \f{\ppi}{2} \Bigr)}$} \\
&= \eqmakebox[first]{$\displaystyle \f{\ppi}{2}$}
- \eqmakebox[second]{$\displaystyle \f{\ppi}{4}$}
+ \eqmakebox[third]{$\displaystyle 2 \f{\ppi}{2}$} \\
&= \f{5}{4} \ppi
\end{align*}

\end{document}

• I like this approach! So here the minus signs and plus signs are not aligned directly; rather their alignment is the inescapeable by-product of aligning the boxes. Is that correct? Jan 28, 2020 at 17:15
• @LinearChristmas: Correct. It's a convenient by-product of correctly sizing with boxes.
– Werner
Jan 28, 2020 at 17:24
• I suspect this approach will therefore work like a charm in this particular case (and, in fact, for most of the document I'm working on). On a possible downside, there might be (not sure atm) a situation which calls for similar alignment inside align´ but for which there are no clear well-definable boxes. Great answer still. (I'll probably accept an answer somewhere around 48 hours from now). Jan 28, 2020 at 17:36
• @LinearChristmas: True. There might be different cases where a box-alignment is not needed, at which point one could use aligned (or similar). This is a convenient approach with your use-case. You can do it manually as well... that is, measure the boxes yourself and then use that in your code. eqparbox just makes this usage convenient by automating the measurement. It uses (La)TeX's \label-\ref-like system, storing content in your .aux. So, any change in the widest element will require two compilations to settle.
– Werner
Jan 28, 2020 at 17:41