RMarkdown: Auto Number Equations - latex

How can I autonumber equations in an Rmarkdown document?
I've tried including "\usepackage{mathajax}" in the header, but the equations won't autonumber. I believe I need to configure mathjax, but can't figure out how.
I'm open to any solutions.
Please note, the solution in this post does not work.
Edit:
A simplified example of the desired output is below.
The code for the above image is below. I tried using \begin{equation} and \end{equation} instad of "align";however, the HTML document does not output the formulas correctly.
---
header-includes:
- \usepackage{amsmath}
output:
html_document: default
---
\begin{align}
2+2 = 4 \tag{1} \\
3+2 = 5 \tag{2} \\
4+2 = 6 \tag{3} \\
5+2=7 \tag{4} \\
\end{align}

You can get pretty close by using bookdown::html_document2, c.f. the documentation:
---
output:
bookdown::html_document2: default
---
\begin{align}
2+2 &= 4 \\
3+2 &= 5 (\#eq:three) \\
4+2 &= 6 (\#eq:four) \\
5+2 &= 7 (\#eq:five)
\end{align}
Result:
You get an equation number for every equation where you put a label. I have omitted the label on the first equation to show this effect. This is slightly different from the LaTeX's behavior, where you have to use \notag in an align environment to not get an equation number.

Related

Stata - Esttab to Latex Decimal Alignment

I am trying to output a LaTex file using Stata's esttab command. I cannot get the decimals to align, nor can I get the nice formatting from the "booktabs" option to work. I also would like to increase the width of the columns. I have been tinkering with this for hours and have had no luck. Below is my Stata code and the LaTex code that I am running through Overleaf. Any help would be greatly appreciated!
Here is my Stata code:
#delimit ;
esttab r1 r2 r3 r4 using "C:\Users\user\Dropbox\Private
Code\Code\STATA\latex\table1.tex", replace
b(3) nomtitle label star(* 0.10 ** 0.05 *** 0.01)
booktabs alignment(D{.}{.}{-1})
title(Placeholder)
s(N DAY FFE FIRM_QUARTER r2_a,
fmt(%9.0fc 0 0 0 3)
label("Observations" "Day FE" "Firm FE" "Firm-Quarter FE" "Adj. R-Sq{\super 2}"))
compress nocons;
Here is the LaTex code it spits out:
\begin{table}[htbp]\centering
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\caption{Placeholder}
\begin{tabular}{l*{4}{D{.}{.}{-1}}}
\toprule
&\multicolumn{1}{c}{(1)} &\multicolumn{1}{c}{(2)} &\multicolumn{1}
{c}{(3)} &\multicolumn{1}{c}{(4)} \\
\midrule
VAR1 & 0.007\sym{***}& 0.007\sym{***}& 0.006\sym{***}& 0.007\sym{***}\\
& (8.55) & (7.82) & (7.67) & (8.54) \\
\addlinespace
VAR2 & 0.132\sym{***}& 0.131\sym{***}& 0.131\sym{***}& 0.132\sym{***}\\
& (20.35) & (20.15) & (20.15) & (20.35) \\
\midrule
Observations &1,315,077 &1,315,077 &1,315,077 &1,315,077
\\
Day FE & No & Yes & Yes & No \\
Firm FE & Yes & No & Yes & No \\
Firm-Quarter FE & No & No & No & Yes \\
Adj. R-Sq{\super 2}& 0.045 & 0.027 & 0.046 & 0.044
\\
\bottomrule
\multicolumn{5}{l}{\footnotesize \textit{t} statistics in parentheses}\\
\multicolumn{5}{l}{\footnotesize \sym{*} \(p<0.10\), \sym{**} \(p<0.05\), \sym{***} \(
p<0.01\)}\\
\end{tabular}
\end{table}
Since you haven't provided the full code, I can't see what would your output be. When I add \documentclass{article} with missing packages in a preamble, I get very wide table which does not fit a page. Also, I don't know whether the code is automatically generated or created/altered by you.
Assuming the latter, I would do the following. Since you add footnotes in the table, I would enclose tabular within threeparttable, which splits the area for table into three parts: top for captions, middle for a table definition e.g. tabular, and bottom part for footnotes. It also adds a convenient macro \tnote{}.
Secondly, dcolumn causes big numbers to be pushed way too much to the left. You should let dcolumn suppress their alignment. I personally don't know how, although I assume there is a way.
I prefer siunitx for formatting numbers in tables. However siunitx needs a bit more work. It automatically parses numbers in cells and any non-number content generates errors. Therefore, non-numbers need to be enclosed within {...} which instructs siunitx to leave the content intact. It also forces numbers not to be formatted, aligned etc., which is what I did for the big numbers you have in the table. There's also option to format thousands and millions with any separator of your choice. For instance you could use one of the macros: \,, :, or \; for thin, mid or thick space, respectively instead of ,.
Below is my suggestion for your table based on siunitx and threeparttable, so you can compare it with your approach.
\documentclass{article}
\usepackage{array}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{threeparttable}
\newcommand\super[1]{\textsuperscript{#1}}
\sisetup{
group-separator={,},
table-format=1.3,
table-align-text-after = false,
}
\begin{document}
\begin{table}[htbp]
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\centering
\begin{threeparttable}
\caption{Placeholder}
\begin{tabular}{l *{4}{S}}
\toprule
& \multicolumn{1}{c}{(1)}
& \multicolumn{1}{c}{(2)}
& \multicolumn{1}{c}{(3)}
& \multicolumn{1}{c}{(4)} \\
\midrule
{VAR1} & 0.007\tnote{***} & 0.007\tnote{***} & 0.006\tnote{***} & 0.007\tnote{***} \\
& {(8.55)} & {(7.82)} & {(7.67)} & {(8.54)} \\
\addlinespace
VAR2 & 0.132\tnote{***} & 0.131\tnote{***} & 0.131\tnote{***} & 0.132\tnote{***} \\
& {(20.35)} & {(20.15)} & {(20.15)} & {(20.35)} \\
\midrule
Observations
& \multicolumn{1}{c}{\num{1315077}}
& \multicolumn{1}{c}{\num{1315077}}
& \multicolumn{1}{c}{\num{1315077}}
& \multicolumn{1}{c}{\num{1315077}} \\
Day FE & {No} & {Yes} & {Yes} & {No} \\
Firm FE & {Yes} & {No} & {Yes} & {No} \\
Firm-Quarter FE & {No} & {No} & {No} & {Yes} \\
Adj. R-Sq{\super2} & 0.045 & 0.027 & 0.046 & 0.044 \\
\bottomrule
\end{tabular}
\par\(t\) statistics in parentheses
\par\tnote{*} \(p < 0.10\),\quad\tnote{**} \(p < 0.05\),\quad\tnote{***} \(p < 0.01\)
\end{threeparttable}
\end{table}
\end{document}
EDIT. I also enclosed statistics in{...} because siunitx does not recognised brackets as part of a number.

Make centered LaTeX tabular table using knitr kable

The knitr::kable with the latex format option produces a tabular environment.
For example,
# produces a tabular environment
knitr::kable(head(cars),
format = 'latex')
produces
\begin{tabular}{r|r}
\hline
speed & dist\\
\hline
4 & 2\\
\hline
4 & 10\\
\hline
7 & 4\\
\hline
7 & 22\\
\hline
8 & 16\\
\hline
9 & 10\\
\hline
\end{tabular}
The table is not centered. If I use kable_stylying from kableExtra, whose value for the option position defaults to center, I get a centered floating table, using the LaTeX table enviornment.
For example, this
# produces a tabular environment inside a table
knitr::kable(head(cars),
format = 'latex') %>%
kableExtra::kable_styling()
produces
\begin{table}
\centering
\begin{tabular}{r|r}
\hline
speed & dist\\
\hline
4 & 2\\
\hline
4 & 10\\
\hline
7 & 4\\
\hline
7 & 22\\
\hline
8 & 16\\
\hline
9 & 10\\
\hline
\end{tabular}
\end{table}
However, I just want to make a small, almost inline, table. I do not want it to float. I do not want captions, etc. What I want then is something to produce LaTeX code like this:
\begin{center}
\begin{tabular}{r|r}
\hline
speed & dist\\
\hline
4 & 2\\
\hline
4 & 10\\
\hline
7 & 4\\
\hline
7 & 22\\
\hline
8 & 16\\
\hline
9 & 10\\
\hline
\end{tabular}
\end{center}
Is that possible using knitr and kableExtra?
I can use a workaround. For example, use chunk options results='asis', and in the chunk, do
cat("\\begin{center}",sep='\n')
knitr::kable(head(cars),
format = 'latex')
cat("\\end{center}",sep='\n')
However, I would like to know if this it is possible without the workaround and whether I am missing something.
Minimal reproducible example
The following is a minimal working example of an RMarkdown document at produces the three kinds of tables mentioned above. Dependencies are magrittr, knitr, and kableExtra.
---
output: pdf_document
---
```{r}
library(magrittr)
```
This produces a `tabular`, non-floating, table. But it is not centered.
```{r}
knitr::kable(head(cars), format = 'latex')
```
The following code produces a centered table, but using a `table` environment, so it floats (in this case to the top of the page), which is probably what we usually want, but we don't *always* want.
```{r}
knitr::kable(head(PlantGrowth), format = 'latex') %>%
kableExtra::kable_styling()
```
We can produce a centered `tabular` environment using chunk option `results='asis'` etc.
```{r, echo=FALSE, results='asis'}
cat("\\begin{center}",sep='\n')
knitr::kable(head(ToothGrowth),
format = 'latex')
cat("\\end{center}",sep='\n')
```
Rendering this (as a pdf_document) creates a single page pdf with this content:
You just need to set the table.envir argument to "center" to wrap the \tabular in \begin{center} ... \end{center}. In your MRE:
---
output: pdf_document
---
```{r}
library(magrittr)
```
This produces a `tabular`, non-floating, table, and it *is* centered:
```{r}
knitr::kable(head(cars), format = 'latex', table.envir = "center")
```
If you want to wrap it twice (e.g. centered and boldface), you can use something like table.envir = c("bf", "center", "bf"): because kable() isn't smart enough to reverse the order when adding the \end{} markers, you need to use a palindrome.
If you use \centering, you'll avoid the extra vertical spacing from the center environment:
---
output:
pdf_document:
keep_tex: true
header-includes:
- \AddToHook{env/tabular/before}{\begingroup\centering}
- \AddToHook{env/tabular/after}{\par\endgroup}
---
```{r}
library(magrittr)
```
text
```{r}
knitr::kable(head(cars), format = 'latex')
```
text

Latex tabu-package

Many years I've successfully used the tabu-package to create tables like shown in the code-example below. I produced an extended number of those tables in an documentation and so want to avoid major changes.
Since the June update of MiKTeX to Version 21.6 all the tables show the same error-message like:
Row xx Dimension too large. \end{longtabu}
As soon as I use one of the previous MiKTeX Version e.g. 21.3. proper tables are produced.
Are there any ideas what the reason could be and how to circumnavigate the problem?
My code:
\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{array}
\usepackage{graphicx}
\usepackage{setspace}
\usepackage{longtable}
\usepackage{tabu}
\begin{document}
\begin{spacing}{2}
\begin{longtabu}to\textwidth{lXl}
200 A & & 300 B \\
100 C & & 200 D \\
30 E & & 600 F \\
80 G & & 50 H \\
0,5 I & &3000 J \\
\end{longtabu}
\end{spacing}
\end{document}

how to write inline matrix in Latex

\begin{pmatrix} 2 \\ 4 \\ -2 \end{pmatrix}
This is 3*1 vector matrix and i want to write another matrix or x and y s beside that one.
Is there any method to write matrix inline ?
Are you using in-line delimiters around the math? E.g.,
$\begin{pmatrix} 2 \\ 4 \\ -2 \end{pmatrix}$
If you put another matrix inside these delimiters, they will be put in-line next to it. For instance, see this live example:
<script>
MathJax = {
tex: {inlineMath: [['$','$']]}
}
</script>
<script src="https://cdn.jsdelivr.net/npm/mathjax#3/es5/tex-chtml.js" id="MathJax-script"></script>
This math is in line, $
\begin{pmatrix}
a & b & c \\
d & e & f \\
g & h & i
\end{pmatrix}
\begin{pmatrix} 2\\ 4\\ -2 \end{pmatrix}
$, with text following it.
Simply put your code inside two dollar signs ($) and it's done.
$ \begin{pmatrix} 2 \\ 4 \\ -2 \end{pmatrix} $
Note that if you put double dollar signs ($$) it will write the matrix in a new line and then continues the rest of the sentence in the line after that.
$$ \begin{pmatrix} 2 \\ 4 \\ -2 \end{pmatrix} $$

Table title with outreg2

I am working with the community-contributed command outreg2 to export regression estimates from a logit model to LaTeX.
Is there a way to show the table reference/label, for example, "Table 1: Some Title"?
I cannot seem to be able to do this using outreg2:
logit v1 v2 v43, or
outreg2 using "table", label cti(odds ratio) eform tex replace ctitle(Logit)
With esttab, using \label in the title option to get the desired result works:
esttab using "Table.tex",tex eform label replace title("Logit"\label{tab1})
Any suggestions?
EDIT:
Note that even when i do the following:
outreg2 using "table", label cti(odds ratio) eform tex replace ///
ctitle(Logit) title("Logit"\label{tab1})
This only produces the following:
Consider the following toy example:
webuse lbw
logit low age lwt i.race smoke ptl ht ui, or
Both esttab and outreg2 produce the same result when their title() option is used:
esttab, tex eform label replace title("Logit"\label{tab1})
outreg2 using "table", tex label cti(odds ratio) eform replace title("Logit"\label{tab1})
However, this will not work when typesetting as outreg2 produces
different markup than esttab.
The way to do what you want with outreg2 is to run the following from Stata:
outreg2 using table, tex replace label cti(odds ratio) eform
Unlike esttab, the outreg2 command does not have an option to insert markup
directly from within Stata. Thus you will have to add the markup below manually:
.
.
.
\begin{table}[htbp]\centering
\caption{Logit \label{tab1}}
.
.
.
\end{table}
.
.
.
The produced tex file should then look as follows:
\documentclass[]{article}
\setlength{\pdfpagewidth}{8.5in} \setlength{\pdfpageheight}{11in}
\begin{document}
\begin{table}[htbp]\centering
\caption{Logit \label{tab1}}
\begin{tabular}{lc} \hline
& (1) \\
VARIABLES & odds ratio \\ \hline
& \\
birthweight<2500g & \\
& \\
age of mother & 0.973 \\
& (0.0355) \\
weight at last menstrual period & 0.985** \\
& (0.00682) \\
race = 2, black & 3.535** \\
& (1.861) \\
race = 3, other & 2.368** \\
& (1.040) \\
smoked during pregnancy & 2.518** \\
& (1.009) \\
premature labor history (count) & 1.719 \\
& (0.595) \\
has history of hypertension & 6.250*** \\
& (4.322) \\
presence, uterine irritability & 2.135* \\
& (0.981) \\
Constant & 1.586 \\
& (1.910) \\
& \\
Observations & 189 \\ \hline
\multicolumn{2}{c}{ seEform in parentheses} \\
\multicolumn{2}{c}{ *** p$<$0.01, ** p$<$0.05, * p$<$0.1} \\
\end{tabular}
\end{table}
\end{document}
Leaving this here for future reference.
Actually, if you add the option tex(fragment) to outreg2, the resulting file will only contain the tabular environment. Then, you can create the table environment, with the caption that you desire, and use \input only to replace the tabular environment.

Resources