6.13 Tables

6.13.1 Tables in Latex

Tables in raw LaTeX can be included in Quarto documents using fenced divs or code chunks with {=latex}.

  • Use fenced div to add labels #tbl-xxx for cross-referencing. Note the # is mandatory. Without it, the table cross reference will show as ?? in the output.
    • You can ignore the fenced divs if you don’t need cross-reference.
  • Refer to the table using @tbl-xxx.
  • The table lable must begin with tbl- no matter whether you use div or latex approach for cross-references.
::: {#tbl-1}

```{=latex} 
% Or whatever actual you want, includegraphics, whatever. 
% (_not_ \begin{table}, just the content of it)
\begin{tabular}{l c r}
\hline
Header 1 & Header 2 & Header 3 \\
\hline
Row 1 Col 1 & Row 1 Col 2 & Row 1 Col 3 \\
Row 2 Col 1 & Row 2 Col 2 & Row 2 Col 3 \\
\hline
\end{tabular}
```

This is a table caption.

:::

For cross-reference: See @tbl-1.

Alternatively, use LaTeX syntax entirely as follows.

The regression results are summarized in Table \ref{tbl-regression-results}.

```{=latex} 
\begin{table}
\centering
\caption{Regression Results: CAPM vs. Fama-French 3-Factor Model}
\label{tbl-regression-results}
\begin{tabular}{l c r}
\hline
Header 1 & Header 2 & Header 3 \\
\hline
Row 1 Col 1 & Row 1 Col 2 & Row 1 Col 3 \\
Row 2 Col 1 & Row 2 Col 2 & Row 2 Col 3 \\
\hline
\end{tabular}
\end{table}
```

Q: How to use [H] to force table placement in qmd?
A: Add the following to the preamble file:

\usepackage{float} % for [H] placement specifier

% Make all tables use [H] placement by default
\makeatletter
\renewenvironment{table}[1][H]{%
  \@float{table}[#1]%
}{%
  \end@float
}
\makeatother

\renewenvironment{table}[1][H] takes one optional argument (the placement specifier) that defaults to H. This means that if you use \begin{table} without specifying a placement, it will default to [H], which forces the table to be placed exactly where it appears in the source code.

You can still override the default placement for individual tables by providing a different specifier, e.g. \begin{table}[htbp] will use the standard htbp placement for that table.

ref: https://github.com/quarto-dev/quarto-cli/discussions/6734#discussioncomment-6919437


6.13.2 Tables in Markdown

You can create tables using standard Markdown syntax.

| Col1 | Col2 | Col3 |
| ---- | ---- | ---- |
| A    | B    | C    |
| E    | F    | G    |
| A    | G    | G    |

: My Caption {#tbl-letters .striped .hover}

See @tbl-letters for the table.
  • Captions are added using Pandoc table syntax: :␣Caption text.

  • Table labels and attributes can be added within curly braces {} after the caption.

    If your table does not have a caption, you can still add attributes by placing them directly after :␣

  • You can also explicitly specify columns widths using the tbl-colwidths attribute or document-level option. If you have a table with two columns, and want to set 1st col to 25% and 2nd col to 75% of the table width, you can do:

    | fruit  | price  |
    |--------|--------|
    | apple  | 2.05   |
    | pear   | 1.37   |
    | orange | 3.09   |
    
    : Fruit prices {tbl-colwidths="[25, 75]"}

ref: https://quarto.org/docs/authoring/tables.html#markdown-tables


6.13.3 Stargazer

An example of a stargazer landscape table that supports both HTML and PDF output.

```{r alterative-models, eval=FALSE}
library(stargazer)

# Run three models
model1 <- lm(mpg ~ wt, data = mtcars)
model2 <- lm(mpg ~ wt + cyl, data = mtcars)
model3 <- lm(mpg ~ wt + cyl + hp, data = mtcars)

# generate a summarizing table using stargazer
is_html <- knitr::is_html_output()
stargazer(
  model1, model2, model3, 
  title = "Regression Results", 
  type = if (is_html) "html" else "latex",
  dep.var.labels = "Miles Per Gallon (mpg)", 
  covariate.labels = c("Weight (lbs)", "Cylinders", "Horsepower"), 
  digits = 2,
  notes = "Standard errors in parentheses.",
  notes.append = TRUE,
  float = is_html, # PDF: emit a bare tabular so it fits inside adjustbox
  font.size = if (is_html) NULL else "small", # PDF: reduce font size
  column.sep.width = if (is_html) "" else "1pt"
)
```

The code above is only displayed (eval=FALSE); the chunk below re-runs it via ref.label and prints the table. For PDF the table sits on a landscape page and is scaled with adjustbox. The raw-LaTeX wrappers are kept as literal {=latex} blocks (not cat() from R) so Quarto passes them through reliably; only the table – not the echoed source code – goes inside adjustbox, otherwise the verbatim listing breaks the box. In HTML these blocks are dropped and a normal table is shown.

Note that you need to load lscale and adjustbox packages in the preamble for PDF output.

::: {.content-visible when-format="pdf"}
```{=latex}
\begin{landscape}
\begin{adjustbox}{width=\linewidth,center}
```
:::

```{r alterative-models-out, ref.label='alterative-models', echo=FALSE, results='asis'} 
```

::: {.content-visible when-format="pdf"}
```{=latex}
\end{adjustbox}
\end{landscape}
```
:::

The fenced divs with {=latex} only appear in the PDF output, and are dropped in HTML output. See .content-visible for more details on how to conditionally show content based on metadata.