I am trying to get aligned equations within a box in rmarkdown. I am knitting to pdf.
The reason the box is necessary is because the YAML metadata I use automatically left-aligns all the text, but I want the equations centered.
Here is my rmarkdown code. As you can see when you run it, the equations are centered but the second set of = signs are not aligned across the two lines.
Any help much appreciated.
---
title: "Aligning equations in a box"
output:
pdf_document: default
html_document: null
word_document: null
toc: yes
linestretch: 1.3
classoption: fleqn
header-includes:
- \setlength{\mathindent}{0pt}
- \setlength\parindent{0pt}
---
\setlength{\abovedisplayskip}{-15pt}
\setlength{\belowdisplayskip}{1pt}
\setlength{\abovedisplayshortskip}{1pt}
\setlength{\belowdisplayshortskip}{1pt}
```{r global_options, include=FALSE, echo = FALSE}
knitr::opts_chunk$set(fig.width=12, fig.height=8, fig.path='Figs/',
echo=FALSE, warning=FALSE, message=FALSE, dev = 'pdf')
```
These words are automatically left-aligned by the YAML meta-data above so a box is necessary to centre equations.
### Equation 11.6
\[\large
\makebox[\linewidth]{$\displaystyle
\begin{aligned}
\text{when}\ PT &= 0:logit\ h(t_{j})\ &= [\alpha_{7}D_{7} + \alpha_{8}D_{8} + \cdots + \alpha_{12}D_{12}]\\
\text{when}\ PT &= 1:logit\ h(t_{j})\ &= [\alpha_{7}D_{7} + \alpha_{8}D_{8} + \cdots + \alpha_{12}D_{12}] + \beta_{1}
\end{aligned}
$}
\]
Not sure I understand, but is it necessary to have a big space between? If not, you can just remove the &,
\[\large
\makebox[\linewidth]{$\displaystyle
\begin{aligned}
\text{when}\ PT =& 0:logit\ h(t_{j})\ = [\alpha_{7}D_{7} + \alpha_{8}D_{8} + \cdots + \alpha_{12}D_{12}]\\
\text{when}\ PT =& 1:logit\ h(t_{j})\ = [\alpha_{7}D_{7} + \alpha_{8}D_{8} + \cdots + \alpha_{12}D_{12}] + \beta_{1}
\end{aligned}
$}
\]
this gives
If you want a space you could use something like \qquad or \quad
\[\large
\makebox[\linewidth]{$\displaystyle
\begin{aligned}
\text{when}\ PT =& 0:logit\ h(t_{j})\ \qquad= [\alpha_{7}D_{7} + \alpha_{8}D_{8} + \cdots + \alpha_{12}D_{12}]\\
\text{when}\ PT =& 1:logit\ h(t_{j})\ \qquad= [\alpha_{7}D_{7} + \alpha_{8}D_{8} + \cdots + \alpha_{12}D_{12}] + \beta_{1}
\end{aligned}
$}
\]
which gives me
EDIT:
Answering the question in your comment. Wrap the blocks in between two &.
\[\large
\makebox[\linewidth]{$\displaystyle
\begin{aligned}
\text{whenmoretext}\ PT &= 0:logit\ h(t_{j})\ & &= [\alpha_{7}D_{7} + \alpha_{8}D_{8} + \cdots + \alpha_{12}D_{12}]\\
\text{when}\ PT &= 1:logit\ h(t_{j})\ & &= [\alpha_{7}D_{7} + \alpha_{8}D_{8} + \cdots + \alpha_{12}D_{12}] + \beta_{1}
\end{aligned}
$}
\]
Related
I want to emulate this in LaTeX code as an equation.
\usepackage{spalign}
\[
\spalignsys{
\text{Maximizar} \hspace{4mm} z=c_1x_1+c_2x_2+\dots+c_nx_n ;
\text{Sujeto a:} ;
\hspace{4mm} a_{11}x_1+a_{12}x_2+\dots+a_{1n}x_n \leq b_1;
x_1,x_2,\dots,x_n \geq 0 }
\]
You can use the same technique as in https://tex.stackexchange.com/a/587258/36296 :
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
\left\{\begin{array}{lc}
\text{Maximizar} & z=c_1x_1+c_2x_2+\dots+c_nx_n\\
\text{Sujeto a:}& \\
&a_{11}x_1+a_{12}x_2+\dots+a_{1n}x_n \leq b_1\\
& x+1+2+3\\
\end{array}\right.
\]
\end{document}
I am trying to generate equation numbers and cross-referencing in docx using Rmarkdown, but it is not working.
Here is my YAML
output:
officedown::rdocx_document:
reference_docx: ref_transfer.docx
header-includes:
- \usepackage{amsmath}
I have tried using (\#eq:test) in the equation chunk enclosed with $$ (code below)
\begin{equation}
\begin{gather}
\text{Y}_{ij} = \alpha_{0} + \beta_{0}\text{X}_{1ij} + \alpha_{1}\text{Z}_{1j} +
\alpha_{2}\text{Z}_{2j} + \beta_{1}\text{Z}_{1j}\text{X}_{1ij} +
\beta_{2}\text{Z}_{2j}\text{X}_{1ij} + v_j\text{X}_{1ij} + u_j + \epsilon_{ij} \\
\text{where} \\
\epsilon_{} \sim N(0, \sigma^2) \\
u_j \sim N(0, \sigma^2_u) \\
v_j \sim N(0, \sigma^2_v)
\end{gather}
(\#eq:test)
\end{equation}
and \#ref(eq:test) to cross-reference in line. That produced #ref(eq:test) in line and (#eq:test) as the equation number, see RenderOutput below.
I have also tried using \label{eq:test} (code below)
\begin{equation}
\begin{gather}
\text{Y}_{ij} = \alpha_{0} + \beta_{0}\text{X}_{1ij} + \alpha_{1}\text{Z}_{1j} +
\alpha_{2}\text{Z}_{2j} + \beta_{1}\text{Z}_{1j}\text{X}_{1ij} +
\beta_{2}\text{Z}_{2j}\text{X}_{1ij} + v_j\text{X}_{1ij} + u_j + \epsilon_{ij} \\
\text{where} \\
\epsilon_{} \sim N(0, \sigma^2) \\
u_j \sim N(0, \sigma^2_u) \\
v_j \sim N(0, \sigma^2_v)
\end{gather}
\label{eq:test}
\end{equation}
in conjunction with \eqref{eq:test} in line.
And that produces blanks where you'd expect to see the equation number and citation, see RenderOutput2.
Does anyone have a work around for this type of problem?? Please help.
To get this the expected output in a word document you will need to use bookdown for the proper numbering and cross-referencing. See where the base_format argument is specified in the yaml section below.
---
output:
officedown::rdocx_document:
base_format: "bookdown::word_document2"
---
\begin{equation}
\begin{gather}
\text{Y}_{ij} = \alpha_{0} + \beta_{0}\text{X}_{1ij} + \alpha_{1}\text{Z}_{1j} +
\alpha_{2}\text{Z}_{2j} + \beta_{1}\text{Z}_{1j}\text{X}_{1ij} +
\beta_{2}\text{Z}_{2j}\text{X}_{1ij} + v_j\text{X}_{1ij} + u_j + \epsilon_{ij} \\
\text{where} \\
\epsilon_{} \sim N(0, \sigma^2) \\
u_j \sim N(0, \sigma^2_u) \\
v_j \sim N(0, \sigma^2_v)
\end{gather}
(\#eq:test)
\end{equation}
This is a cross-reference \#ref(eq:test)
And the results I got:
Consider the following class diagram:
\documentclass{article}
\usepackage{geometry}
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{empheq}
\usepackage{mdframed}
\usepackage{booktabs}
\usepackage{lipsum}
\usepackage{graphicx}
\usepackage{color}
\usepackage{psfrag}
\usepackage{pgfplots}
\usepackage{bm}
\usepackage{tikz}
\usepackage{tikz-uml}
\begin{document}
\begin{figure}[h!]
\centering
\begin{tikzpicture}
\umlclass[]{Class1}
{
}
{
}
\umlinterface[y=4]{Class2}{}
{
+ createDocument(doc: str) : str \\
+ resolveDocument(id: str) : str \\
+ updateDocument(doc: str) : str \\
+ deleteDocument(id: str) : str \\
+ createStatusList(sl: str) \\
+ resolveStatusList(id: str) \\
+ updateStatusList(sl: str) \\
+ deleteStatusList(id: str)
}
\umlclass[x=-2,y=-3]{Class33333333333333333333333}{}{}
\umlclass[x=3,y=-3]{Class4}{}{}
\umlinherit{Class1}{Class2}
\umlaggreg[geometry=|-|]{Class1}{Class33333333333333333333333}
\umlaggreg[geometry=|-|]{Class1}{Class4}
\end{tikzpicture}
\end{figure}
\end{document}
Since Class3 is bigger in size I had to add a unit for the x coordinate of Class2 to keep some space between the two. Is this the correct approach? How can I get even space between classes by not playing with coordinates? Every time a class changes in size I often need to redefine many coords. Also, how to fix the alignment of the two classes in respect of Class 1?
You can use relative positioning from the TikZ positioning library:
\documentclass{article}
\usepackage{geometry}
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{empheq}
\usepackage{mdframed}
\usepackage{booktabs}
\usepackage{lipsum}
\usepackage{graphicx}
\usepackage{color}
\usepackage{psfrag}
\usepackage{pgfplots}
\usepackage{bm}
\usepackage{tikz}
\usepackage{tikz-uml}
\usetikzlibrary{positioning}
\begin{document}
\begin{figure}[h!]
\centering
\begin{tikzpicture}
\umlinterface{Class2}{}
{
+ createDocument(doc: str) : str \\
+ resolveDocument(id: str) : str \\
+ updateDocument(doc: str) : str \\
+ deleteDocument(id: str) : str \\
+ createStatusList(sl: str) \\
+ resolveStatusList(id: str) \\
+ updateStatusList(sl: str) \\
+ deleteStatusList(id: str)
}
\umlclass[below=1cm of Class2]{Class1}
{
}
{
}
\umlclass[below left=2cm and 0.5cm of Class1]{Class33333333333333333333333}{}{}
\umlclass[below right=2cm and 0.5cm of Class1]{Class4}{}{}
\umlinherit{Class1}{Class2}
\umlaggreg[geometry=|-|]{Class1}{Class33333333333333333333333}
\umlaggreg[geometry=|-|]{Class1}{Class4}
\end{tikzpicture}
\end{figure}
\end{document}
\begin{algorithm}
\caption{Sinkhorn's algorithm}
\begin{algorithmic}
\WHILE{ $\| B(u_{k}, v_{k})1 - \mu \| + \| B(u_{k}, v_{k})^{T} 1 - \nu \| \ge \epsilon'$ }
\IF {$k \text{ mod } 2 = 0$}
\STATE $u_{k+1} = u_{k} + ln \left( \frac{\mu}{B(u_{k},v_{k})\mathrm{1}} \right)$
\STATE $v_{k+1} = v_{k}$
\ELSE
\STATE $v_{k+1} = v_{k} + ln \left( \frac{\nu}{B(u_{k},v_{k})^{T} \mathrm{1}} \right) $
\STATE $u_{k+1} = u_{k}$
\ENDIF
\STATE $k = k+1$
\end{algorithmic}
\end{algorithm}
This code doesn't work do you know why ?
Thanks and regards.
\ENDWHILE should be used to indicate the end of the loop.
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{algorithm}
\usepackage{algorithmic}
\usepackage{amsmath}
\begin{document}
\begin{algorithm}
\caption{Sinkhorn's algorithm}
\begin{algorithmic}
\WHILE{ $\| B(u_{k}, v_{k})1 - \mu \| + \| B(u_{k}, v_{k})^{T} 1 - \nu \| \ge \epsilon'$ }
\IF {$k \text{ mod } 2 = 0$}
\STATE $u_{k+1} = u_{k} + ln \left( \frac{\mu}{B(u_{k},v_{k})\mathrm{1}} \right)$
\STATE $v_{k+1} = v_{k}$
\ELSE
\STATE $v_{k+1} = v_{k} + ln \left( \frac{\nu}{B(u_{k},v_{k})^{T} \mathrm{1}} \right) $
\STATE $u_{k+1} = u_{k}$
\ENDIF
\STATE $k = k+1$
\ENDWHILE
\end{algorithmic}
\end{algorithm}
\end{document}
I'm trying to align this system of equations nicely but it doesn't work out. I think I could hack it but I keep running into this problem and would like to do it right. fiddle
\begin{align}
a_{11}x_1 + a_{12}x_2 + a_{13}x_3 + \cdots + a_{1n}x_n &= 0 \\
a_{21}x_1 + a_{22}x_2 + a_{23}x_3 + \cdots + a_{2n}x_n &= 0 \\
a_{31}x_1 + a_{32}x_2 + a_{33}x_3 + \cdots + a_{3n}x_n &= 0 \\
&\vdots \\
a_{m1}x_1 + a_{m2}x_2 + a_{m3}x_3 + \cdots + a_{mn}x_n &= 0
\end{align}
You can set this in a single align and adjust for the spacing using some \phantoms and overlapping:
\begin{align}
\phantom{a_{m1}x_1}\llap{a_{11}x_1\,} +
\phantom{a_{m2}x_2}\llap{a_{12}x_2\,} +
\phantom{a_{m3}x_3}\llap{a_{13}x_3\,} + \cdots +
\phantom{a_{mn}x_n}\llap{a_{1n}x_n\,} &= 0 \\
\phantom{a_{m1}x_1}\llap{a_{21}x_1\,} +
\phantom{a_{m2}x_2}\llap{a_{22}x_2\,} +
\phantom{a_{m3}x_3}\llap{a_{23}x_3\,} + \cdots +
\phantom{a_{mn}x_n}\llap{a_{2n}x_n\,} &= 0 \\
\phantom{a_{m1}x_1}\llap{a_{31}x_1\,} +
\phantom{a_{m2}x_2}\llap{a_{32}x_2\,} +
\phantom{a_{m3}x_3}\llap{a_{33}x_3\,} + \cdots +
\phantom{a_{mn}x_n}\llap{a_{3n}x_n\,} &= 0 \\
& \phantom{{}={}}\llap{\vdots~~} \\
a_{m1}x_1 + a_{m2}x_2 + a_{m3}x_3 + \cdots + a_{mn}x_n &= 0
\end{align}
Fiddle
\phantom{<stuff>} sets <stuff> in an invisible box thereby taking up the space of <stuff> (horizontally and vertically) without setting anything visually. \llap{<stuff>} puts <stuff> in a zero-width box that is right-aligned. This effectively lets <stuff> hang or overlap to the left.
Additional alignment tweaks are made possible by \, - a small horizontal space.
In align and similar environments, vertical alignment is performed along the & characters in each line. So, if you want to align along each + sign, equip them with & each:
EDIT: Since align centers the rows, you need to surround the + with & on both sides:
\begin{align}
a_{11}x_1 &+& a_{12}x_2 &+& a_{13}x_3 &+& \cdots &+& a_{1n}x_n &=& 0 \\
a_{21}x_1 &+& a_{22}x_2 &+& a_{23}x_3 &+& \cdots &+& a_{2n}x_n &=& 0 \\
a_{31}x_1 &+& a_{32}x_2 &+& a_{33}x_3 &+& \cdots &+& a_{3n}x_n &=& 0 \\
&&&&&&&&&\vdots \\
a_{m1}x_1 &+& a_{m2}x_2 &+& a_{m3}x_3 &+& \cdots &+& a_{mn}x_n &=& 0
\end{align}
Alternatively, you could use \[\begin{array}{lllll}...\end{array}\] as an environment, left-aligning the individual columns.