Math & ScienceHow to align parts inside two "cases" environments ?

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

How to align parts inside two "cases" environments ?

Post by Cham »

I need to align some elements inside two cases definitions. Here's a working code showing my problem :

Code: Select all

\documentclass[12pt,letterpaper,twoside]{book}
\usepackage{lmodern}
\usepackage{amsmath}

\begin{document}

	\begin{align}
		a(t) &=
		\begin{cases}
			\, a_0 \: e^{\lambda \, t}, &\text{if $x < 0$.} \\[12pt]
			\, \alpha \, t^p, &\text{if $x > 0$.}
		\end{cases}
	\intertext{Bla bla bla bla bla :}
		H(t) &=
		\begin{cases}
			\, \lambda, &\qquad \text{if $x < 0$.} \\[12pt]
			\, \displaystyle{\frac{p}{t}}, &\qquad \text{if $x > 0$.}
		\end{cases}
	\end{align}

\end{document}
Preview :
cases.jpg
cases.jpg (14.02 KiB) Viewed 11673 times
As you can see, the equal signs are well aligned. But I also want to align the "if" inside both cases environments. Is that possible, without tweaking and hacking the spaces by eye ? Currently, I used a \qquad command, but the result is looking approximate. I want to fix this.

EDIT : For unknown reason, the overleaf doesn't compile the code above. I don't see where there may be a mistake in my code, which is compiling fine on my system.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

How to align parts inside two "cases" environments ?

Post by Johannes_B »

I wuldn't bother aligning them, honestly.

Code: Select all

\documentclass[12pt,letterpaper,twoside]{book}
\usepackage{lmodern}
\usepackage{mathtools}

\begin{document}

\begin{align}
	a(t) &=
	\begin{cases}
		\, a_0 \: e^{\lambda \, t}, &\text{if $x < 0$.} \\[12pt]
		\, \alpha \, t^p, &\text{if $x > 0$.} \\
		\alpha\beta \sum\lim \mathrm{lalala}, & \text{if friday}
	\end{cases}
	\intertext{Bla bla bla bla bla :}
	H(t) &=
	\begin{cases}
		\mathrlap{\, \lambda, }
		\hphantom{\, a_0 \: e^{\lambda \, t}, }
		&\text{if $x < 0$.} \\[12pt]
		\, \displaystyle{\frac{p}{t}}, &\text{if $x > 0$.}\\
		\mathrlap{0,}
		\hphantom{\alpha\beta \sum\lim \mathrm{lalala},}
		& \text{if not friday}
	\end{cases}
\end{align}

\end{document}

The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

How to align parts inside two "cases" environments ?

Post by Cham »

I was thinking about the \mathrlap and \hphantom trick, but would have prefered something more "elegant", less "hacky" (or maybe it is elegant, actually).

So there's no other way of doing this ?

I think that aligning these parts gives a much nicer output to the eye :
output.jpg
output.jpg (47.73 KiB) Viewed 11667 times
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

How to align parts inside two "cases" environments ?

Post by Johannes_B »

What is prety what is hacky what is the difference?

Fact is (not sure), nobody has predefined it so far. You need to know the length of the longest element (like for a decent description environment), and get this length over to the part, where you need it. You could calculate the width of the actual content, substract tht from the longest content and just add the space that you need. But who does that, if you can have something with zero width?
Following a similar solution. The mechanism is cleaner (safer) from a UX point of view, but it is really doing pretty much the same.

Code: Select all

\documentclass[12pt,letterpaper,twoside]{book}
\usepackage{lmodern}
\usepackage{mathtools}

\newlength{\casealign}
\newcommand{\printLongest}[1]{\settowidth{\global\casealign}{$#1$}#1}
\newcommand{\continueToAlign}[1]{\mathrlap{#1}\hspace{\casealign}}
\begin{document}


\begin{align}
	a(t) &=
	\begin{cases}
		\, a_0 \: e^{\lambda \, t}, &\text{if $x < 0$.} \\[12pt]
		\, \alpha \, t^p, &\text{if $x > 0$.} \\
		\printLongest{\alpha\beta \sum\lim  \mathrm{This is not math},} & \text{if friday}
	\end{cases}
	\intertext{Bla bla bla bla bla :}
	H(t) &=
	\begin{cases}
		\continueToAlign{\, \lambda, }
		&\text{if $x < 0$.} \\[12pt]
		\, \displaystyle{\frac{p}{t}}, &\text{if $x > 0$.}\\
	\end{cases}
\end{align}

\end{document}
Disclaimer: If the stuff is larger at the bottom, you are screwed.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Re: How to align parts inside two "cases" environments ?

Post by Cham »

Thanks a lot Johannes. I much prefer the first solution, since I rarely need to do this. Actually, it's the first and probably the last time I have this special alignment problem !

I think that this case is closed now. Thanks again for your help ! :)
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

How to align parts inside two "cases" environments ?

Post by Johannes_B »

Well, now that i did it, i am shaaring it. It detects screwing your document.

Code: Select all

\documentclass[12pt,letterpaper,twoside]{book}
\usepackage{lmodern}
\usepackage{mathtools}
\usepackage{calc}

\newlength{\casealign}
\newlength{\checklength}
\newcommand{\printLongest}[1]{\settowidth{\global\casealign}{$#1$}#1}
\newcommand{\continueToAlign}[1]{%
\settowidth{\global\checklength}{$#1$}%
\ifdim\checklength>\casealign%
\GenericError{}{case alignment exploded}{I told you you will be screwed}{Come back to LC ;-)}%
\else%
\mathrlap{#1}\hspace{\casealign}%
\fi%
}
\newcommand{\FutureLength}[1]{\settowidth{\global\casealign}{$#1$}}
\begin{document}


\begin{align}
	a(t) &=
	\begin{cases}
		\, a_0 \: e^{\lambda \, t}, &\text{if $x < 0$.} \\[12pt]
		\, \alpha \, t^p, &\text{if $x > 0$.} \\
		\printLongest{\alpha\beta ,\mathrm{This is not math}} & \text{if friday}
	\end{cases}
	\intertext{Bla bla bla bla bla :}
	H(t) &=
	\begin{cases}
		\continueToAlign{\, \lambda, \sum\lim  }
		&\text{if $x < 0$.} \\[12pt]
		\, \displaystyle{\frac{p}{t}}, &\text{if $x > 0$.}\\
	\end{cases}
\end{align}

\begin{align}
	\FutureLength{This is a test}
	a(t) &=
	\begin{cases}
		\, a_0 \: e^{\lambda \, t}, &\text{if $x < 0$.} \\[12pt]
		\, \alpha \, t^p, &\text{if $x > 0$.} \\
		\continueToAlign{a test} & \text{if friday}
	\end{cases}
	\intertext{Bla bla bla bla bla :}
	H(t) &=
	\begin{cases}
		\continueToAlign{this is a test}
		&\text{if $x < 0$.} \\[12pt]
		\, \displaystyle{\frac{p}{t}}, &\text{if $x > 0$.}\\
	\end{cases}
\end{align}
\end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
Post Reply