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.

  • 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
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}
  • 1
    Ummm, you were 30 sec faster ... – AboAmmar Aug 15 '18 at 11:21
  • @AboAmmar It happens ;) – Phelype Oleinik Aug 15 '18 at 11:25
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*).
  • Based on your post here tex.stackexchange.com/questions/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
  • 1
    @rook1996 Sorry, the correct command is \fleqn[0pt] – egreg Aug 22 '18 at 14:43
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

  • I'll give you +1 for the slowness :-) of 30 sec. – Sebastiano Aug 15 '18 at 11:28
  • Haha.. thanks, this time you won by a nose. Best thing actually about tex.sx is how people are helpful and responsive. – AboAmmar Aug 15 '18 at 11:42
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

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.