How to align piecewise functions - latex

I thought a properly aligned piecewise function should be something like this:
Commas and character xs are perfectly aligned in two lines.
I tried
\vert x \vert =
\begin{cases}
x, &x \geq 0 \\
-x, &x < 0 \\
\end{cases}
which only aligned the domain parts like this:
In that way, the commas come after x and -x is not aligned, which looks strange.
I tried to solve that problem by adding spaces preceding the x in first line, which is:
\vert x \vert =
\begin{cases}
\enspace \; x, &x \geq 0 \\
-x, &x < 0 \\
\end{cases}
It gives the correct result (as above) but this solution seems rather strange, if the second line changes, then the first line have also to be modified to keep the spaces fit. I reckon there must be some better way to do it.

The default alignment behaviour of cases is left-left for the value and domain components. In this specific case (for aesthetic reasons), you can insert a \phantom negation to align the values:
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
\lvert x \rvert = \begin{cases}
x, & x \geq 0 \\
-x, & x < 0
\end{cases}
\]
\[
\lvert x \rvert = \begin{cases}
\phantom{-}x, & x \geq 0 \\
-x, & x < 0
\end{cases}
\]
\end{document}

Related

Changing dimensions (height and width ) of latex table generated tabularray

I need to make the LaTeX table generated here smaller in height either by changing the total height or each row height, whichever looks better. How can I do that?
Here is the code:
\begin{table}[h!]
\centering
\caption[Transformation table]{Transformations generated by Mach et al. Y in a dark cell: highly recommended, Y in a light cell: usable, N in a dark cell: unusable \label{tab:power}}
\begin{tblr}{
colspec={lXXXXX},
vlines,
hlines,
cells={halign=c},
column{1-2}={halign=l},
vspan=even,
cell{1}{2}={halign=c,bg=gray,fg=white},
cell{2}{1}={bg=gray,fg=white},
cell{3}{5-6}={bg=lightgray,font=\bfseries},
cell{5}{4-6}={bg=lightgray,font=\bfseries},
cell{6}{3}={bg=lightgray,font=\bfseries},
}
& \SetCell[c=5]{} Type of Distribution & & & &\\
\SetCell[r=5]{} \rotatebox{90}{\parbox{3.5cm}{\centering Type of Transformation}} & & Lognormal & Exponential & Gamma & Weibull \\
& Box-Cox & Y & Y & Y & Y \\
& Exponential & Y & Y & Y & Y\\
& Simple power & Y & Y & Y & Y \\
& Logarithmic & Y & Y & N & N \\
\end{tblr}
\end{table}
The extra height comes from the \parbox. If you reduce the width of the \parbox a bit, the rows become less tall.
If you need the rows to be even narrower, you can change the rowsep (but you'll have to ditch the idea of rotated text then).
\documentclass[11pt]{article}
\usepackage{tabularray}
\usepackage{geometry}
\usepackage{xcolor}
\usepackage{graphicx}
\begin{document}
\begin{table}[h!]
\centering
\caption[Transformation table]{Transformations generated by Mach et al. Y in a dark cell: highly recommended, Y in a light cell: usable, N in a dark cell: unusable \label{tab:power}}
\begin{tblr}{
colspec={lXXXXX},
vlines,
hlines,
cells={halign=c},
column{1-2}={halign=l},
vspan=even,
cell{1}{2}={halign=c,bg=gray,fg=white},
cell{2}{1}={bg=gray,fg=white},
cell{3}{5-6}={bg=lightgray,font=\bfseries},
cell{5}{4-6}={bg=lightgray,font=\bfseries},
cell{6}{3}={bg=lightgray,font=\bfseries},
% rowsep = 0mm
}
& \SetCell[c=5]{} Type of Distribution & & & &\\
\SetCell[r=5]{} \rotatebox{90}{\parbox{2.6cm}{\centering Type of Transformation}} & & Lognormal & Exponential & Gamma & Weibull \\
& Box-Cox & Y & Y & Y & Y \\
& Exponential & Y & Y & Y & Y\\
& Simple power & Y & Y & Y & Y \\
& Logarithmic & Y & Y & N & N \\
\end{tblr}
\end{table}
\end{document}

Aligning two equations with a 'multiline' bracket around them to previous equations

I am trying to align a set of solutions of an equation to previous equations at the position of their equal signs. Next to the set equations is a bracket that goes over both lines. The code I already have is equivalent to the following:
\usepackage{amsmath}
\begin{align*}
x^2 & = 1 \\
\Rightarrow \quad \left\{
\begin{array}{r l}
x & = - 1 \\
x & = 1
\end{array}
\right. \\
x & = 1
\end{align*}
Output
Is there or can I set another marker to use for alignment? Are there other equation environments or solutions for the bracket itself that would make this obsolete?
Additionally, is there an option to reduce the space left to the equal signs of the last two equations to 'normal' size (to comparison I copied the last solution inside the normal equation environment)?
Edit: The last problem can be fixed by using aligned instead of array.
Edit 2: Answer provided here!

Remove indent in equation LaTex

I have a rather simple problem. I have an unwanted indent in my equation.
\documentclass[11 pt, a4paper, fleqn]{article}
\usepackage{float}
\usepackage{amssymb}
\usepackage{amsmath}
\setlength{\mathindent}{0cm}
\begin{document}
\noindent Through rigorous analytical calculations found in \textbf{Appendix A} a state space representation was found:
\begin{multline}
X= \begin{pmatrix}\dot{x_1} \\ \dot{x_2} \\ \dot{x_3} \end{pmatrix} =
\begin{pmatrix}0 & 1 & 0\\ 0 & 0 & 1 \\ -1000 & -300 & -30\end{pmatrix}
\begin{pmatrix}x_1 \\ x_2 \\ x_3 \end{pmatrix}+\begin{pmatrix} 0 \\ 0 \\ 160 \end{pmatrix}u \\
\noindent Y=\begin{pmatrix}1 & 0 & 0 \end{pmatrix} \begin{pmatrix}
x_1 \\ x_2 \\ x_3 \end{pmatrix}
\end{multline}
\end{document}
Which give me this result:
enter image description here
I would like to have the second line at the left margin.
I have tried using \[ \begin{aligned} \end{aligned} \] and at the before I begin my document I have the command \setlength{\mathindent}{0cm} but still the indent persists. It's driving me quite mad.
Please and thank you!
edit: made compileable
You can use:
\begin{align}
X &= \begin{pmatrix}\dot{x_1} \\ \dot{x_2} \\ \dot{x_3} \end{pmatrix} =
\begin{pmatrix}0 & 1 & 0\\ 0 & 0 & 1 \\ -1000 & -300 & -30\end{pmatrix}
\begin{pmatrix}x_1 \\ x_2 \\ x_3 \end{pmatrix}+\begin{pmatrix} 0 \\ 0 \\ 160 \end{pmatrix}u \\
Y &=\begin{pmatrix}1 & 0 & 0 \end{pmatrix} \begin{pmatrix}
x_1 \\ x_2 \\ x_3 \end{pmatrix}
\end{align}
Use align* if you don't want line numbers.
I hope that works!
The multiline environment is specifically meant for a single equation which is too long for one line. The first line will align to the left, the second to the right.
If you want to align multiple different equations, I would use something like the align environment as #HelloWorld shows in their answer.
However just for completeness, you can also do this with multiline:
\documentclass[11 pt, a4paper, fleqn]{article}
\usepackage{float}
\usepackage{amssymb}
\usepackage{amsmath}
\setlength{\mathindent}{0cm}
\begin{document}
\noindent Through rigorous analytical calculations found in \textbf{Appendix A} a state space representation was found:
\begin{multline}
X= \begin{pmatrix}\dot{x_1} \\ \dot{x_2} \\ \dot{x_3} \end{pmatrix} =
\begin{pmatrix}0 & 1 & 0\\ 0 & 0 & 1 \\ -1000 & -300 & -30\end{pmatrix}
\begin{pmatrix}x_1 \\ x_2 \\ x_3 \end{pmatrix}+\begin{pmatrix} 0 \\ 0 \\ 160 \end{pmatrix}u \\
Y=\begin{pmatrix}1 & 0 & 0 \end{pmatrix} \begin{pmatrix}
x_1 \\ x_2 \\ x_3 \end{pmatrix}\hfill
\end{multline}
\end{document}

LaTeX - tabular start at new line

I want to start the tabular at start of line, but when I put it directly after e.g. \paragraph{Foo}, than It will be displayed at right side of 'Foo'.
\documentclass[twocolumn,a4,titlepage]{article}
\begin{document}
\section{foo}
\paragraph{A}
\begin{tabular}{ccc}
X & Y & Z \\
X & Y & Z \\
X & Y & Z \\
\end{tabular}
\paragraph{B}
\begin{tabular}{ccc}
X & Y & Z \\
X & Y & Z \\
X & Y & Z \\
\end{tabular}
\end{document}
I know, I could put it between \begin{center} and \end{center}, but than it would float around (i.e. will sometimes be displayed after the following text) and will be centered, which looks strange for small tables.
I strongly believe that this question is already answered elsewhere, but I couldn't find it.
Thanks!!
You can force the table to start in a new line like this:
\documentclass[twocolumn,a4,titlepage]{article}
\begin{document}
\section{foo}
\paragraph{A}
\begin{tabular}{ccc}
X & Y & Z \\
X & Y & Z \\
X & Y & Z \\
\end{tabular}
\paragraph{B}\mbox{}\par
\noindent\begin{tabular}{#{}ccc}
X & Y & Z \\
X & Y & Z \\
X & Y & Z \\
\end{tabular}
\end{document}

Alternative to Bordermatrix for MathJax

I'm using bordermatrix to write matrix with coefficient on the top and on the left:
I would like to display a similar matrix on my website where I use Mathjax but I get a [Math Processing Error]. Do you have a MathJax-alternative to bordermatrix to get the same result?
You can use the following construction:
\begin{align}
f(x) &= ax^2 + bx + c \\
&= \begin{array}{c c}
& \begin{array} {#{} c c c #{}}
u_1 & \cdots & u_q
\end{array} \\
\begin{array}{c}
e_1 \\ \vdots \\ e_n
\end{array}\hspace{-1em} &
\left(
\begin{array}{#{} c c c #{}}
u_{11} & \cdots & u_{1q} \\
\vdots & & \vdots \\
u_{n1} & \cdots & u_{nq}
\end{array}
\right) \\
\mbox{} % Blank line to match column names so as to align the = vertically
\end{array} \\[-12pt] % Correction for blank line
&= ax^2 + bx + c
\end{align}
Depending on whether you're aligning it with other content, you might not need the vertical adjustment I inserted.
Tested on Math.SE.

Resources