How to control width of a long LaTeX equation in bookdown - latex

I have an align equation that does not fit into my bookdown, especially the tag I am using:
My output is bookdown::gitbook. It works in R markdown though:
---
title: "LaTeX"
output: html_document
---
# Long Equation
This is too long
$$ \begin{align}
\text{outcome} &= f(\text{explanatory}) + \text{noise} \tag{Generic statistical model} \\
\text{outcome} &= \text{intercept} + \text{slope} \cdot \text{explanatory} + \text{noise} \tag{Generic linear model} \\
\end{align}$$
What is the difference between markdown and bookdown in handling LaTeX equations? And how to control the width of the aligned equations? Can I shift the entire formula block to the left?

You can use a div tag with margin right property.
<div style="margin-right:400px;">
\begin{align}
\text{outcome} &= f(\text{explanatory}) + \text{noise} \tag{Generic statistical model} \\
\text{outcome} &= \text{intercept} + \text{slope} \cdot \text{explanatory} + \text{noise} \tag{Generic linear model} \\
\end{align}
</div>
Result:

Related

Aligning tags in a table [Latex] [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 days ago.
Improve this question
I am using the following code to create a table in Latex:
\begin{table}[]
\centering
\renewcommand{\arraystretch}{1}
\caption{Example table.}
\label{tab:model-testbed}
\renewcommand{\arraystretch}{1.1}
\resizebox{\linewidth}{!}{%
\begin{tabular}{|c|l|l|}
\hline
Comment & \multicolumn{1}{c|}{Device1} & \multicolumn{1}{c|}{Device2} \\ \hline
\begin{tabular}[c]{#{}c#{}}BlaBla \\ Sth \end{tabular} &
\begin{tabular}[c]{#{}l#{}}
\begin{equation}
\tag{1}\label{t3:1}
\begin{aligned}
y(x) = 66.1 + 1.11x - 1.28 \times 10^{-8}x^2 \\ +15.08 \times 10^{-2}x^1
\end{aligned}
\end{equation}
\end{tabular} &
\begin{tabular}[c]{#{}l#{}}
\begin{equation}
\tag{2}\label{t3:2}
\begin{aligned}
y(x) =20.33 + 1.24y - 1.74 \times 10^{-1}x^5 \\ + 1.14 \times 10^{-7}y^5
\end{aligned}
\end{equation}
\end{tabular} \\ \hline
\begin{tabular}[c]{#{}c#{}}BlaBla \\ Sth\end{tabular} &
\begin{equation}
\tag{3}\label{t3:3}
\begin{aligned}
w(x) =2.13 + 0.03x
\end{aligned}
\end{equation}
&
\begin{equation}
\tag{4} \label{t3:4}
\begin{aligned}
er(x) =4.85 + 33.56y
\end{aligned}
\end{equation}
\\ \hline
\begin{tabular}[c]{#{}c#{}}BlaBla\\ Sth\end{tabular} &
\begin{equation}
\tag{5}\label{t3:5}
\begin{aligned}
b(x) =12.2 + 5.20y
\end{aligned}
\end{equation}
&
\begin{equation}
\tag{6}\label{t3:6}
\begin{aligned}
a(x) =1.11 + 1.11x - 2.543 \times 10^{-2}x^2
\end{aligned}
\end{equation}
\hline
\end{tabular}}
\end{table}
And it outputs the following table:
I am struggling to find a way how to align the tags. As you can see next to each formula there is a tag (1), (3), etc. , and it is positioned right next to the equation. But, I would like all the tags to be aligned. I tried with \hspace{1cm} to try to move them to the left, hoping that I can position them to the end of the column and with it align them, but it did not work.
Does anyone have an idea how I could do it?
You would need to wrap "display" equations inside minipages. The tags would correctly be pushed to the right. However, equations get centred w.r.t. surrounding environment.
One way to achieve left alignment with right tags is to add equations in-line and then use \tag{} separately. However, the latter is not allowed outside equations. It can be sorted out by manually setting the #currentlabel for very next \label{} usage; I have made a macro for that--its optional star removes parentheses in case you don't want them.
A couple remarks. I added geometry to increase the page width for demonstration because this table gets quite wide. Sometimes, environments like equations cause issues with the standard vertical spacing and this needs a manual adjustment. There are other table-related packages that sort it out, e.g. tabularray but here I have used a strut: \xmathstrut{factor} from mathtools. Also, for multi lined texts, I'd suggest to use \makecell from makecell--a very convenient macro that allows line breaks. EDIT. the vertical spacing could also be sorted by increasing so-called "gape" inside makecells--I'll leave it for you.
Here's the example
\documentclass{article}
\usepackage[margin=2cm]{geometry}
\usepackage{array}
\usepackage{makecell}
\usepackage{mathtools}
\makeatletter
\NewDocumentCommand\refanchor{sm}{
% #1 star removes partheses
% #2 reference label
\IfBooleanF{#1}{(}#2\IfBooleanF{#1}{)}
\protected#edef\#currentlabel{#2}}
\makeatother
\begin{document}
References: eq.~\ref{t3:1} on page \pageref{t3:1}.
References with parentheses: eq.~\eqref{t3:3} on page \pageref{t3:3}.
\begin{table}[tbh]
\renewcommand{\arraystretch}{1.5}
\small
\centering
\caption{Example table.}\label{tab:model-testbed}
\begin{tabular}{|c|l#{}|l#{}|}
\hline
Comment
& \multicolumn{1}{c|}{Device1}
& \multicolumn{1}{c|}{Device2} \\
\hline
\makecell{BlaBla\\Sth}
&
\begin{minipage}{0.4\linewidth}
\(\xmathstrut{1.25}
\begin{aligned}
y(x) = 66.1 + 1.11x - 1.28 \times 10^{-8}x^2 \\
+15.08 \times 10^{-2}x^1
\end{aligned}\)%
\hfill
\refanchor{1}\label{t3:1}
\end{minipage}
&
\begin{minipage}{0.4\linewidth}
\(
\begin{aligned}
y(x) =20.33 + 1.24y - 1.74 \times 10^{-1}x^5\\
+1.14 \times 10^{-7}y^5
\end{aligned}\)
\hfill
\refanchor{2}\label{t3:2}
\end{minipage} \\
\hline
\makecell{BlaBla\\Sth}
&
\begin{minipage}{0.4\linewidth}
\(\xmathstrut{1.15} w(x) =2.13 + 0.03x \)%
\hfill
\refanchor{3}\label{t3:3}
\end{minipage}
&
\begin{minipage}{0.4\linewidth}
\(er(x) = 4.85 + 33.56y\)%
\hfill
\refanchor*{4}\label{t3:4}
\end{minipage}
\\ \hline
\end{tabular}
\end{table}
\end{document}

How to align equation systems in LaTeX?

I'm new to LaTeX and I'm having issues with aligning this equation system. I'm trying to align it in a way such that the x's, +'s, y's and ='s are all aligned. I don't know if that's possible or not, but I couldn't find help online.
I've tried using \systeme*, this way it aligned the ='s.
\[
\systeme*{ax + by=c,dx^2 + ey^2=f}
\]
My next attempt was to use &, which I've noticed you can only use once to change the alignment.
\[
\systeme*{ax& + by=c,dx&^2 + ey^2=f}
\]
This way it aligned the x's.
Thanks for you help.
You can use the alignat environment from amsmath to align multiple points along the same line. So for instance, with your example, you could use the alignat environment with double ampersands in front of each alignment character:
\usepackage{amsmath}
\begin{document}
\begin{alignat*}{5}
&a && x && + b && y && =c\\
&d && x^2 && + e && y^2 && =f
\end{alignat*}
\end{document}
This produces an aligned result:
Another alternative: the environment array where you can align each column left (l), centered (c) or right (r).
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
\begin{array}{llll}
ax & + & by & = c\\
dx^2 & + & ey^2 & = f
\end{array}
\]
\end{document}
Here I aligned all of the four columns to the left ({llll}).

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.

Equation numbering in colab notebook

I'm using
\begin{equation}.. \end{equation} in markdown mode in a Colab notebook and I don't get equation numbering as I would expect from Latex.
Is there a fix for this?
One option would be to use \tag{}, e.g.
\begin{equation}
y = x \tag{1}
\end{equation}
or
\begin{align}
f(x) &= x \tag{1}, \\
g(x) &= x^2 + 2. \tag{2}
\end{align}

Latex Long Equations Alignment in single column

I need multiple equations aligned in single column of a two columned document. I have tried a mixture of both align and multiline but it does not work.
\documentclass[12pt,journal,compsoc]{IEEEtran}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\begin{document}
\maketitle
\section{Introduction}
\begin{align*}
p(W_{i},T_{n})&=\begin{multline*}
p(W_{i},T_{n})\\+(p(W_{i},T_{n})*k/100)
\end{multline*}\\
p(W_{i},T_{n})&=\begin{multline*}
p(W_{i},T_{n})\\+(p(W_{i},T_{n})*k/100)
\end{multline*}\\
\end{align*}
\end{document}
It gives me the error
Package amsmath Error :\begin{multline*} allowed only in paragraph mode
There's no need for multline here; a regular align will do:
\documentclass[journal,compsoc]{IEEEtran}
\usepackage{amsmath,lipsum}
\begin{document}
\section{Introduction}
\lipsum*[1]
\begin{align*}
p(W_i, T_n) ={}& p(W_i, T_n) \\
& + (p(W_i, T_n) \times k / 100) \\
p(W_i, T_n) ={}& p(W_i, T_n) \\
& + (p(W_i, T_n) \times k / 100)
\end{align*}
\lipsum*[2]
\end{document}
Since you're aligning with a line-break, it's easier to use ={}& as opposed to the traditional &= (which would require additional \phantoms to ensure proper alignment).

Resources