7

There are several post on multiple alignments, but I cant find a solution regarding my problem.

%%%%% Dokumentenklasse mit verschiedenen Attributen
\documentclass[a4paper, abstracton, bibtotocnumbered,liststotoc,12pt]{scrartcl}

%%%%% Betriebssystemabhängige Eingabedekodierung
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc} %% für Windows
\usepackage[ngerman]{babel}
\usepackage{amsthm}
\usepackage{mathtools}

\newcommand{\matr}[1]{\mathbf{#1}} 
\newcommand{\X}{\matr{X}} %Matrix von X
\newcommand{\y}{\matr{y}} %y als voller Vektor
\newcommand{\Xt}{\X^\mathbf{T}} %Matrix von X
\newcommand{\x}{\matr{x}}%Regressor von X
\newcommand{\xt}{\x^\mathbf{T}}%Regressor von X
\newcommand{\betahat}{\hat{\beta}} %betahat


\begin{document}
\begin{flalign*}
\Xt\y-\Xt\X\betahat &= \lambda s\\
\Leftrightarrow \underbrace{\Xt \y}_{p \times 1} &= \underbrace{\vphantom{ \Xt \y } \lambda s}_{p \times 1}, \quad s \in [-1,1]^{p} \\
\intertext{\textnormal{Das $j$-te Element entspricht}}
\xt_{j}\y &= \lambda s_j, \quad \quad s_j \in [-1,1] \Leftrightarrow \lambda s_j \in [-\lambda, \lambda] \\
\Leftrightarrow  |\xt_{j}\y| &\leq \lambda, \quad \forall j \in \{1,...,p\}
\end{flalign*}
\end{document}

I am using the following code and I want to multiple align the staments that are following after the comma and \quad. I tried to multilpe align them with && but the space between the equations and the other arguments will be far too large. I roughly want the same space as produced by quad but with an alignment of the arguments.

rook1996
  • 289
  • 2
    If you look at published mathematical literature (books and papers) you will find that almost no one uses equivalence arrows (\Leftrightarrow) at the beginning of each line. Whether that is because people think it is a bad idea in general or whether they just can't get it done sensibly in LaTeX is probably open for interpretation. – moewe Aug 15 '18 at 11:17
  • 2
    That said, maybe alignat is for you: \begin{alignat*}{2} \Xt\y-\Xt\X\betahat &= \lambda s\\ \Leftrightarrow \underbrace{\Xt \y}_{p \times 1} &= \underbrace{\vphantom{ \Xt \y } \lambda s}_{p \times 1}, &&\quad s \in [-1,1]^{p} \\ \intertext{\textnormal{Das $j$-te Element entspricht}} \xt_{j}\y &= \lambda s_j, &&\quad s_j \in [-1,1] \Leftrightarrow \lambda s_j \in [-\lambda, \lambda] \\ \Leftrightarrow |\xt_{j}\y| &\leq \lambda, &&\quad \forall j \in \{1,...,p\} \end{alignat*} – moewe Aug 15 '18 at 11:18

4 Answers4

10

For multiple alignment points you can use alignat:

enter image description here

\documentclass[a4paper, abstracton, bibtotocnumbered,liststotoc,12pt]{scrartcl}

%%%%% Betriebssystemabhängige Eingabedekodierung
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc} %% für Windows
\usepackage[ngerman]{babel}
\usepackage{amsthm}
\usepackage{mathtools}

\newcommand{\matr}[1]{\mathbf{#1}} 
\newcommand{\X}{\matr{X}} %Matrix von X
\newcommand{\y}{\matr{y}} %y als voller Vektor
\newcommand{\Xt}{\X^\mathbf{T}} %Matrix von X
\newcommand{\x}{\matr{x}}%Regressor von X
\newcommand{\xt}{\x^\mathbf{T}}%Regressor von X
\newcommand{\betahat}{\hat{\beta}} %betahat

\begin{document}
\begin{alignat*}{2}
  \Xt\y-\Xt\X\betahat & = \lambda s\\
  \Leftrightarrow \underbrace{\Xt \y}_{p \times 1} & = \underbrace{\vphantom{ \Xt \y } \lambda s}_{p \times 1}, & \quad & s \in [-1,1]^{p} \\
  \intertext{\textnormal{Das $j$-te Element entspricht}}
  \xt_{j}\y &= \lambda s_j, & \quad & s_j \in [-1,1] \Leftrightarrow \lambda s_j \in [-\lambda, \lambda] \\
  \Leftrightarrow |\xt_{j}\y| &\leq \lambda, & \quad & \forall j \in \{1,\dots,p\}
\end{alignat*}
\end{document}
7

The flalign environment’s name means full length align, which means that the whole line width will be used when there is more than one alignment point.

In order to control the alignment you can use alignat or just align if you don't want to bother.

%%%%% Dokumentenklasse mit verschiedenen Attributen
\documentclass[a4paper, abstracton,12pt]{scrartcl}

%%%%% Betriebssystemabhängige Eingabedekodierung
\usepackage[T1]{fontenc}
%\usepackage[latin1]{inputenc} %% für Windows
\usepackage[ngerman]{babel}
\usepackage{amsthm}
\usepackage{mathtools}

\newcommand{\matr}[1]{\mathbf{#1}} 
\newcommand{\X}{\matr{X}} %Matrix von X
\newcommand{\y}{\matr{y}} %y als voller Vektor
\newcommand{\Xt}{\X^\mathbf{T}} %Matrix von X
\newcommand{\x}{\matr{x}}%Regressor von X
\newcommand{\xt}{\x^\mathbf{T}}%Regressor von X
\newcommand{\betahat}{\hat{\beta}} %betahat


\begin{document}

\section{\texttt{alignat}}
\begin{alignat*}{2}
\Xt\y-\Xt\X\betahat
  &= \lambda s\\
\Leftrightarrow {\underbrace{\Xt \y}_{p \times 1}}
  &= {\underbrace{\vphantom{ \Xt \y } \lambda s}_{p \times 1}}, 
  &\quad& s \in [-1,1]^{p} \\
\intertext{Das $j$-te Element entspricht}
\xt_{j}\y
  &= \lambda s_j,
  &\quad& s_j \in [-1,1] \Leftrightarrow \lambda s_j \in [-\lambda, \lambda] \\
\Leftrightarrow  |\xt_{j}\y|
  &\leq \lambda,
  &\quad& \forall j \in \{1,\dots,p\}
\end{alignat*}

\section{\texttt{align}}
\begin{align*}
\Xt\y-\Xt\X\betahat
  &= \lambda s\\
\Leftrightarrow {\underbrace{\Xt \y}_{p \times 1}}
  &= {\underbrace{\vphantom{ \Xt \y } \lambda s}_{p \times 1}}, 
  && s \in [-1,1]^{p} \\
\intertext{Das $j$-te Element entspricht}
\xt_{j}\y
  &= \lambda s_j,
  && s_j \in [-1,1] \Leftrightarrow \lambda s_j \in [-\lambda, \lambda] \\
\Leftrightarrow  |\xt_{j}\y|
  &\leq \lambda,
  && \forall j \in \{1,\dots,p\}
\end{align*}

\end{document}

enter image description here

A few points to note.

  1. latin1 is not “for Windows”. Use it if the file is encoded in Latin-1, but you should switch to UTF-8 as soon as possible.
  2. The options bibtotocnumbered and liststotoc are deprecated.
  3. \underbrace{<code>}_{<code>} should be braced in order to keep the correct spacing, so {\underbrace{<code>}_{<code>}}.
  4. Never use ... in the input, but \dots or, if needed, one among \dotsc, \dotsb or \dotsm (see Difference of the \dots*).
egreg
  • 1,121,712
  • Based on your post here https://tex.stackexchange.com/questions/446939/the-correct-layout-for-mathematical-derivations?noredirect=1#comment1122745_446939, how can I left aligned all the first equation inside alignat – rook1996 Aug 21 '18 at 12:52
  • 1
    @rook1996 I’ll check later – egreg Aug 21 '18 at 13:20
  • @rook1996 I'm not sure about left alignment: this should be a global decision, which can be obtained with the fleqn option. – egreg Aug 21 '18 at 18:59
  • just need it for the Appendix part and not global – rook1996 Aug 21 '18 at 19:00
  • @rook1996 Try with \usepackage{nccmath} in the preamble and \leqn[0pt] after \appendix. – egreg Aug 21 '18 at 19:05
  • Results into an error. I've changed now all my other derivations, thar are basing on flalign (and aligned) environments to be left aligned. However this derivation is still pending because of the alignat environment. May the informations, that only this derivation have to be left aligned helps somehow – rook1996 Aug 22 '18 at 13:17
  • 1
    @rook1996 Sorry, the correct command is \fleqn[0pt] – egreg Aug 22 '18 at 14:43
  • Nice one ! Is it possible to use the \fleqn just inside one section and then remove to the old "layout" ? – rook1996 Aug 22 '18 at 15:47
  • @rook1996 \ceqn, but in that case I'd prefer the environment form, see the documentation of nccmath. – egreg Aug 22 '18 at 16:35
6

You need alignat*:

\documentclass[a4paper, abstracton, bibtotocnumbered,liststotoc,12pt]{scrartcl}

%%%%% Betriebssystemabhängige Eingabedekodierung
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc} %% für Windows
\usepackage[ngerman]{babel}
\usepackage{amsthm}
\usepackage{mathtools}

\newcommand{\matr}[1]{\mathbf{#1}} 
\newcommand{\X}{\matr{X}} %Matrix von X
\newcommand{\y}{\matr{y}} %y als voller Vektor
\newcommand{\Xt}{\X^\mathbf{T}} %Matrix von X
\newcommand{\x}{\matr{x}}%Regressor von X
\newcommand{\xt}{\x^\mathbf{T}}%Regressor von X
\newcommand{\betahat}{\hat{\beta}} %betahat


\begin{document}
\begin{alignat*}{2}
\Xt\y-\Xt\X\betahat &= \lambda s && \\
\Leftrightarrow \underbrace{\Xt \y}_{p \times 1} &= \underbrace{\vphantom{\Xt\y}\lambda s}_{p\times1}, &&\quad s \in [-1,1]^{p} \\
\intertext{\textnormal{Das $j$-te Element entspricht}}
\xt_{j}\y &= \lambda s_j, &&\quad s_j \in [-1,1] \Leftrightarrow \lambda s_j \in [-\lambda, \lambda] \\
\Leftrightarrow  |\xt_{j}\y| &\leq \lambda, &&\quad \forall j \in \{1,...,p\}
\end{alignat*}
\end{document}

enter image description here

AboAmmar
  • 46,352
  • 4
  • 58
  • 127
4

Some layout improvements (at least for my taste): I used \underbracket from mathtools in preference to \underbrace which doesn't look so nice for small formulæ. Also, I used \ArrowBetweenLines (from mathtools again) – this adds another column in alignat. Last, I don't see why the transposed symbol should be in boldface, so I used \mathrm.

\documentclass[a4paper, abstracton, bibtotocnumbered,liststotoc,12pt]{scrartcl}

%%%%% Betriebssystemabhängige Eingabedekodierung
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc} %% für Windows
\usepackage[ngerman]{babel}
\usepackage{mathtools}

\newcommand{\matr}[1]{\mathbf{#1}}
\newcommand{\X}{\matr{X}} %Matrix von X
\newcommand{\y}{\matr{y}} %y als voller Vektor
\newcommand{\Xt}{\X^\mathrm{T}} %Matrix von X
\newcommand{\x}{\matr{x}}%Regressor von X
\newcommand{\xt}{\x^\mathrm{T}}%Regressor von X
\newcommand{\betahat}{\hat{\beta}} %betahat

\begin{document}

\begin{alignat*}{3}
  & & \Xt\y-\Xt\X\betahat & = \lambda s\\
\ArrowBetweenLines
  & & \underbracket[0.4pt]{\Xt \y}_{p \times 1} & = \mkern-2mu\underbracket[0.4pt]{\mkern2mu \vphantom{\y}\lambda s\mkern1mu}_{p \times 1}\mkern-1mu, & \quad & s \in [-1,1]^{p} \\
  \intertext{\textnormal{Das $j$-te Element entspricht}}
 & & \xt_{j}\y &= \lambda s_j, & \quad & s_j \in [-1,1] \Leftrightarrow \lambda s_j \in [-\lambda, \lambda] \\
\ArrowBetweenLines
  & & |\xt_{j}\y| &\leq \lambda, & \quad & \forall j \in \{1,\dots,p\}
\end{alignat*}

\end{document} 

enter image description here

Bernard
  • 271,350