6.13 Tables

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

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


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