Left Align lines of multi-line MathJax - latex

I have a set of equations where I want the first two lines to be left aligned and the remaining right align. I have the right alignment working but I cannot figure out how to get the first two equations to left align. I am using the latest MathJax (3.1.0) and Hugo (v0.78.2-959724F0 windows/amd64)
Note: This question does not address this. That formats ALL MathJax to the left of a page. I just need these two lines to align left.
$$
Maximize: 5.00 x_{1} + 7.50 x_{2} \\\\
Subject\ to: \\\\
\begin{align}
x_{1} \leq& 200 \\\\
x_{2} \leq& 300 \\\\
1.0x_{1} + 1.5x_{2}\leq& 650 \\\\
x_{1}, x_{2} \geq& 0
\end{align}
$$
Displays this:
But I want it to look like this:

I found that you really need to separate it into two sections to have two different align sections:
$$
\begin{align}
& \text{Maximize: } 5.00 x_{1} + 7.50 x_{2} \\
& \text{Subject to:} \\
\end{align}
$$
$$
\begin{align}
&x_{1}& &\leq200 \\
&&x_{2} &\leq 300 \\
1.0&x_{1} + &1.5x_{2}&\leq 650 \\
&&x_{1}, x_{2} &\geq0
\end{align}
$$
Will render this (you may need to double the \ depending on your environment to get newlines to work):

Just to share ideas: Why not an HTML-table approach?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>MathJax</title>
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax#3/es5/tex-mml-chtml.js"></script>
<style>
td.tdleftc{text-align:center;min-width:80px}
td.tdleftl{text-align:left;min-width:80px}
td.tdright{text-align:right}
</style>
</head>
<body>
<table>
<tr><td class="tdleftl">Maximize:</td><td class="tdright">\(5.00\,x_1 + 7.50\,x_2\)</td></tr>
<tr><td class="tdleftl">Subject to:</td><td class="tdright"></td></tr>
<tr><td class="tdleftc">\(x_1\)</td><td class="tdright">\(\leq 200\)</td></tr>
<tr><td class="tdleftc"></td><td class="tdright">\(x_2 \leq 300\)</td></tr>
<tr><td class="tdleftc">\(1.0\,x_1 +\)</td><td class="tdright">\(1.5\,x_2 \leq 650\)</td></tr>
<tr><td class="tdleftc"></td><td class="tdright">\(x_1, x_2 \geq 0\phantom{00}\)</td></tr>
</table>
</body>
</html>
I feel that the content we are discussing would fit better in a tabular layout rather than in an math environment: since here we somehow embed one markup into another, I would leave the LaTeX syntax to the single cells and even style (align) the content of each cell by using CSS.

Related

\multicolumn alternative in KaTeX

I want to be able to align a single cell's contents differently from the rest. I've found that this can be done using \multicolumn in LaTeX however, I have yet to find an alternative that works in KaTeX. Below is roughly what my current table looks like. Say that I want to align the cell with "Hello" to the right, how would I go about that in KaTeX?
\begin{array}{|l|}
\hline
\text{Hi} \\
\hline
\text{Good Morning} \\
\hline
\text{Hello} \\
\hline
\text{Howdy} \\
\hline
\end{array}
Indeed in KaTeX you can't do all that you can do in LaTeX, even using a tabular environment is not trivial.
As long as your array or table must only conain text, I would rely on html <table> tag and CSS style:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Array?</title>
<style>
table,td{border:1px solid black;border-collapse:collapse}
td.cellle,td.cellri{padding:5px}
td.cellri{text-align:right}
</style>
</head>
<body>
<table>
<tr><td class="cellle">Hi</td></tr>
<tr><td class="cellle">Good Morning</td></tr>
<tr><td class="cellri">Hello</td></tr>
<tr><td class="cellle">Howdy</td></tr>
</table>
</body>
</html>
table,td{border:1px solid black;border-collapse:collapse}
td.cellle,td.cellri{padding:5px}
td.cellri{text-align:right}
<table>
<tr><td class="cellle">Hi</td></tr>
<tr><td class="cellle">Good Morning</td></tr>
<tr><td class="cellri">Hello</td></tr>
<tr><td class="cellle">Howdy</td></tr>
</table>
Also, if it is needed, you can then use the KaTeX inline math delimiters within any single cell, whose alignment you control by the CSS.

Typesetting the numbered mathematical formula in latex

How to typeset the following numbered mathematical formula:
Use amsmath's cases environment:
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
\Delta(x) = \begin{cases}
1 & \text{if $x > 0$} \\
-1 & \text{if $x < 0$} \\
0 & \text{otherwise}
\end{cases}
\]
\end{document}
Here is one way to do it:
$
\Delta(x)=
\left\{
\begin{matrix}
1 & \mathrm{if}~x>0 \\
-1 & \mathrm{if}~x<0 \\
0 & \mathrm{otherwise}
\end{matrix}
\right.
$
Which produces the equation you wanted like this:
The example LaTeX equation
This type of equation has a few key tricks in it:
The left bracket has to be declared as \{ in order for LaTeX to
know to display it since it is a special character
Also, the left brace has to be resized to match however many rows you include in your equation, which is done by the \left command preceding it. This can also be done for (, [, etc.
Since you have a \left command, it has to be accompanied by a \right}, but you don't want to display the right brace, so the \right. ends the section without displaying the right brace.
The \mathrm command turns the text inside it back to normal text (removing the math mode temporarily) so that your "if" and "otherwise" aren't italicized.
The ~ just adds a little space so that your "if" doesn't collide with the x > 0, which often happens when using mathrm
Alternatively, you can check out this post for other ways to write piecewise functions in LaTeX. I hope this helps.

Display latex table/tabular in Jupyter with latex cell magic

According to this Rich Display System example the Jupyter notebook can display latex using the %%latex cell magic.
The example given using the align environment works fine on my system (Notebook Server 3.2.0-8b0eef4) but when I try and show a tabular or a table, the result is just nicely typesetting Latex code!
Is there some kind of preamble I need to add to make this work?
Answer
Jupyter builds on MathJax and cite "MathJax doesn't implement tabular". The link also shows the recommended array environment as tabular replacement.
Workaround:
Latex can also be used in Markdown cells (Celltype Markdown instead of Code). You can select the left area besides the cell and press "m" key or via Cell>Cell-Type in top menu. Then you could use html for the table.
<table>
<tr>
<td>
\begin{eqnarray}
\nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \\
\nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\
\end{eqnarray}
</td>
<td>
\begin{eqnarray}
\nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\
\nabla \cdot \vec{\mathbf{B}} & = 0
\end{eqnarray}
</td>
</tr>
</table>
Markdown cells are rendered so you don't see the code that generates the rendered latex. Instead you just see the rendered version until you double click it.
Plus you have the option of Markdown tables

MathJax numbering equations wrongly

MathJax is numbering equation's lines that are given with \cr, \newline or \\ separately where it should be a single number for whole equation.
The equation;
\begin{align}
\ket{\lo}\ket{\lo} & \underset{U_f}{\to} \ket{\lo} \ket{\lo \oplus f(\lo)} \cr
\ket{\li}\ket{\lo} & \underset{U_f}{\to} \ket{\li} \ket{\lo \oplus f(\li)} \cr
\ket{\lo}\ket{\li} & \underset{U_f}{\to} \ket{\lo} \ket{\li \oplus f(\lo)} \cr
\ket{\li}\ket{\li} & \underset{U_f}{\to} \ket{\li} \ket{\li \oplus f(\li)} \, .
\end{align}
Renders as:
the original pdf:
is there a way to prevent MathJax to number equation's aligned lines?
and this is how MathJax numbering configured
MathJax.Hub.Config({
TeX: { equationNumbers: { autoNumber: "AMS" } }
});
thanks

Collapsible minibox environment in LaTeX

I'm trying to learn LaTeX, currently because otherwise, my professors will be nearly unable to read my homework assignments. I've come across something I want to do, but don't seem to be able to, ie. I have searched google (possibly with a poor keyword set) and not found a solution.
The specific case is as follows: I want to put an ams flalign environment inside a box and have multiple such environments side by side. I have achieved this using minipage, but minipage asks for a width. I would like to use the smallest width in which the flalign environment fits. I realize that I can set the width to 0pt, but I can't help wondering if there's something that is intended to do this.
Also, should I be using minipage? Is there another command I don't know?
Thanks for your reply.
EDIT:
An attempted clarification as to what I want to do. I want equations which are standard, known, given, etc. and short on the left. To the right of those, I want relevant derived equations (and maybe their derivations. Further right, I want actual calculations plugged in.
I feel like what I want is a tabular environment with 3 columns, but I don't think I can put an equation environment in a tabular environment.
This looks like what I want when I render it.
\begin{minipage}[t]{0pt}
\begin{flalign*}
\sigma & = F / A&\\
A & = \pi \left(d/2\right)^2&\\
\epsilon &= \frac{\sigma}{E}&\\
\epsilon_{trans} &= - \nu \epsilon_{longi}& \\
\epsilon &= \frac{\Delta l}{l}&\\
l &= \left( \epsilon + 1 \right) \times l_0&
\end{flalign*}
\end{minipage}
\hspace*{0pt}
\begin{minipage}[t]{0pt}
\begin{flalign*}
d & = \unit[1.8]{mm} = \unit[1.8\e{-3}]{m} &\\
F_T & = \unit[1300]{N}&\\
E_{\text{stainless steel}}&=\unit[193\e9]{Pa}&\\
l_0 & = \unit[.2530]{m}&\\
\nu & = .33&\\
\sigma &= \frac{\unit[1300]{N}}{\pi \times \unit[3.24\e{-6}]{m^2}}&&= \boxed{\unit[127.7\e6]{Pa}}\\
&&&=\boxed{\unit[18,524]{psi}}\\
\epsilon &= \frac{\unit[127.7\e6]{Pa}}{\unit[193\e9]{Pa}} &&= \boxed{6.6\e{-2}}\\
\epsilon_{trans} &= -.33 \times 6.6\e{-2} &&=\boxed{-2.2\e{-2}}\\
l &= \left( 6.6\e{-2} + 1 \right) \times \unit[.2530]{m} &&= \boxed{\unit[.2797]{m}}
\end{flalign*}
\end{minipage}
I'm not sure exactly what you're trying to achieve, but amsmath's align* environment might do what you want (without resorting to minipages):
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{align*}
x&=y & X&=Y & a&=b+c & mn&=ab\\
x’&=y’ & X’&=Y’ & a’&=b & m'n'&=a'b'\\
x+x’&=y+y’ & X+X’&=Y+Y’ & a’b&=c’b & m'&=a'
\end{align*}
\end{document}
As to your minipage question: it requires a width because TeX needs to know where to break the lines. If you don't want the line-breaking algorithm to be used, you probably don't want a minipage.
Edit:
If you want multiple columns and don't care about the vertical alignment of material across the columns, that can be obtained easily enough with the multicols package:
\documentclass{article}
\usepackage{multicols}
\usepackage{lipsum}% just for some example text
\begin{document}
% The * version allows the columns to have ragged bottoms.
% The argument 2 is the number of columns.
\begin{multicols*}{2}
\lipsum[1]% one paragraph of Lorem ipsum.. filler text
\vfil% fills the remainder of the column with white space
\columnbreak% force a column break
\lipsum[2]% another paragraph of text
\vfil% fills the remainder of the column with white space
\end{multicols*}
\end{document}
You might find something to help you in the empheq and mathtools packages. empheq allows you to box equations and mathtools should provide some useful environments for stacking them horizontally.

Resources