# Latex Empirical Tables

> >-

- **Type:** Skill
- **Install:** `agentstack add skill-kennethkhoocy-applied-micro-skills-latex-empirical-tables`
- **Verified:** Yes — security-reviewed for prompt injection and unsafe behavior
- **Seller:** [kennethkhoocy](https://agentstack.voostack.com/s/kennethkhoocy)
- **Installs:** 0
- **Category:** [Agent Skills](https://agentstack.voostack.com/c/agent-skills)
- **Latest version:** 0.1.0
- **License:** MIT
- **Upstream author:** [kennethkhoocy](https://github.com/kennethkhoocy)
- **Source:** https://github.com/kennethkhoocy/applied-micro-skills/tree/main/plugins/applied-micro/skills/latex-empirical-tables

## Install

```sh
agentstack add skill-kennethkhoocy-applied-micro-skills-latex-empirical-tables
```

Requires the [AgentStack CLI](https://agentstack.voostack.com/docs/cli). Works with Claude Code, Cursor, and any MCP-compatible agent.

## About

# LaTeX Empirical Tables

Empirical economics and finance papers export regression and estimation output as estout/esttab "body fragments" — rows of `\multicolumn` headers, coefficients with `\sym` stars, and parenthesised p-values or SEs, with no `\toprule`/`\bottomrule` — pulled into the manuscript through a wrapper macro (`\estauto`/`\estwide`/`\estautoLT`). Done well this reads like a top-journal table. Done naively it looks broken: wide tables spill past the right margin, the note legend is jammed inside the tabular as a boxed cell, columns sit at uneven widths, and `\input`-ed fragments throw "Misplaced \omit". This skill sets a results `.tex` up from scratch, converts ugly estout tables to the gold-standard pattern, and diagnoses the `\omit` error. The reasoning behind each choice is given so you can adapt rather than copy blindly.

## Routing

| You want to... | Go to |
|----------------|-------|
| Set up `results.tex`/`main.tex` and its preamble from scratch | Section A |
| Fix or reformat existing estout tables (overflow, cramped notes, stars, alignment) | Section B |
| Diagnose a "Misplaced `\omit`" error from an `\input`-ed fragment | Section C |
| Merge several self-compiling block `results.tex` files into one master document | Section D |
| Fix skipped table numbers (Table 1, Table 3, ...) when `\captionof` + `longtable` are combined | Section D |

---

# Section A — Build a results document from scratch

When producing regression or estimation tables, follow this workflow to create a
compilable LaTeX document with a standard empirical-economics preamble and estout macros.

## Directory structure

All output lives under `outline/` relative to the project root:

```
outline/
├── preamble/
│   └── preamble.tex    ← LaTeX preamble (no \begin{document})
├── Tables/
│   ├── table_01.tex    ← table fragments (auto-increment)
│   ├── table_02.tex
│   └── ...
├── Figures/
│   └── ...             ← figures (.pdf, .png, etc.)
└── results.tex         ← master document (created on first table)
```

## Step-by-step workflow

### 1. Ensure directories exist

Create `outline/preamble/` and `outline/Tables/` if they don't already exist. Also
create `outline/Figures/` if figures will be produced.

### 2. Save the preamble

Copy `references/preamble.tex` from this skill's directory to
`outline/preamble/preamble.tex`. This file contains everything up to but NOT including
`\begin{document}` — the document class, all packages, and the estout macro definitions
(`\estauto`, `\estwide`, `\estautoLT`, `\sym`, `\specialcell`). The macros are defined
there in full; Section B lists a minimal subset if you only need to graft them onto an
existing preamble.

Do NOT create `outline/results.tex` yet — it gets created automatically when the first
table is generated.

### 3. When producing a table

Each time a regression or summary table is generated:

1. Save the table fragment to `outline/Tables/table_XX.tex` with auto-incrementing
   numbering (01, 02, 03, ...).

2. If `outline/results.tex` does **not** exist, create it:
   ```latex
   \input{preamble/preamble}

   \begin{document}

   \section{Tables}

   \subsection{Descriptive Title}
   \begin{table}[H]
   \caption{Descriptive Title}
   \centering
   \estauto{Tables/table_01.tex}{N}{D{.}{.}{-1}}
   \end{table}
   \FloatBarrier

   \end{document}
   ```

3. If `outline/results.tex` **already exists**, insert the following block BEFORE
   `\end{document}`:
   ```latex
   \subsection{Descriptive Title}
   \begin{table}[H]
   \caption{Descriptive Title}
   \centering
   \estauto{Tables/table_XX.tex}{N}{D{.}{.}{-1}}
   \end{table}
   \FloatBarrier
   ```

Where `N` = number of data columns in the fragment (excluding the label column).

## Estout table macros

Three macros are defined in the preamble for including table fragments generated by any
tool (pyfixest, esttab, R, etc.). All use the TeX primitive `\@@input` to avoid
`\multicolumn`/`\omit` errors (Section C explains why this matters).

| Macro | Use case | Environment |
|-------|----------|-------------|
| `\estauto{file}{ncols}{colspec}` | Standard-width tables | `tabular` |
| `\estwide{file}{ncols}{colspec}` | Full-page-width tables | `tabular*` |
| `\estautoLT{file}{ncols}{colspec}` | Multi-page tables | `longtable` |

The default column spec is `D{.}{.}{-1}` (decimal-aligned via `dcolumn`). Use `c` only
when columns contain non-numeric content.

## Table fragment format

Fragments must follow these conventions — the wrapper macros add `\toprule` and
`\bottomrule`, so fragments should NOT include them.

### Structure:
- Column headers: `\multicolumn{1}{c}{(1)}` style
- Group headers: `\multicolumn{N}{c}{Group Name}` with `\cmidrule(lr){start-end}`
- Coefficients right-aligned, p-values in parentheses on the next line
- Significance: `\sym{*}` p\omit`
- **Scenario**: A tabular environment uses `\input{file.tex}` where the file starts with
  `\multicolumn{N}{...}{...}`
- **Pattern**: Multi-panel tables with `\multicolumn{N}{l}{\textbf{Panel A:...}}` as the
  first content line fail, while single-panel tables starting with
  ` & \multicolumn{1}{c}{(1)} & ...` succeed (because `\omit` is in the second cell, not
  the first)
- **Key diagnostic**: The exact same table content compiles without error when pasted
  inline instead of `\input`-ed

## Root Cause

LaTeX's `\input` command is NOT the TeX primitive. LaTeX redefines `\input` to wrap the
primitive (`\@@input`) with:
- File existence checks (`\InputIfFileExists`)
- Path resolution
- Nested input tracking

These wrapper operations are **non-expandable** and execute as content in the first table cell. Once any content has been processed in a cell, `\multicolumn`'s internal `\omit` command (which must be the absolute first token in a cell) is rejected by TeX's alignment mechanism.

**Why some tables work**: Fragments starting with ` & \multicolumn{1}{c}{...}` put `\omit` in the *second* cell (after `&`), where it IS the first token. Only fragments where `\multicolumn` spanning all columns is the very first token trigger the error.

## Solution

Replace LaTeX's `\input` with the TeX primitive `\@@input` in the table macro. The
primitive is fully expandable and injects no tokens before the file content.

### Before (broken):

```latex
\newcommand{\estauto}[3]{
    \vspace{.75ex}{
        \begin{tabular}{l*{#2}{#3}}
        \toprule
        \estinput{#1}
        \\ \bottomrule
        \addlinespace[.75ex]
        \end{tabular}
    }
}
\let\estinput=\input
```

### After (fixed):

```latex
\makeatletter
\newcommand{\estauto}[3]{%
    \vspace{.75ex}{%
        \begin{tabular}{l*{#2}{#3}}\toprule\@@input #1 %
        \\ \bottomrule
        \addlinespace[.75ex]
        \end{tabular}%
    }%
}
\makeatother
```

### Key details of the fix:

1. **`\@@input` instead of `\input`**: The TeX primitive is fully expandable, so it
   injects no tokens before the file content. Requires `\makeatletter` / `\makeatother`
   since the name contains `@`.

2. **Syntax**: `\@@input #1 ` (space-terminated filename, no braces). The trailing space
   after `#1` is the filename terminator for the primitive.

3. **Same line**: `\toprule\@@input #1 %` keeps everything on one line with `%` eating the
   trailing newline, preventing whitespace tokens between `\toprule` and the file content.

4. **`%` after lines**: Add `%` at end of macro definition lines to prevent newline-generated
   space tokens from entering the tabular cells.

## Also fix: trailing `\\` in fragments

When using an `\estauto`-style macro that appends `\\ \bottomrule` after the input, ensure
table fragment files do NOT end with `\\` on their last line. Double `\\` before
`\bottomrule` creates an empty row where `\bottomrule`'s internal `\omit`/`\multispan` also
fails.

## Verification

1. Compile with `pdflatex -interaction=nonstopmode`
2. Check log: `grep "^!" results.log` should return nothing
3. Previously failing multi-panel tables now compile cleanly
4. PDF output should be visually identical

## Notes

- This is a fundamental TeX/LaTeX interaction issue, not specific to any particular package
  or document class
- The `booktabs` package (`\toprule`, `\midrule`, `\bottomrule`) is NOT the cause, though
  it commonly appears in the error context
- The issue affects ANY `\input` inside a tabular where `\multicolumn` is the first token,
  regardless of `\toprule`
- MiKTeX, TeX Live, and other distributions are all affected
- `\let\estinput=\input` does NOT help because it aliases LaTeX's redefined `\input`, not
  the primitive
- In nonstopmode, TeX ignores the error and produces correct output, but the errors halt
  compilation in interactive/scrollmode (the default)

---

# Section D — Assemble a master document from self-compiling block folders

Verified 2026-07-20 assembling seven exhibit blocks (each a self-compiling `results.tex` +
`Tables\` + `Figures\` + block-local `preamble\`) into one `exhibits.tex`. A small script
(`make_master.py`) extracts each block's body and concatenates them under one shared preamble.
Three pitfalls bit in practice; all three fixes below are load-bearing.

## The extraction recipe

For each block, take the text between `\begin{document}` and `\end{document}`, then rewrite
resource paths into the block subfolder. THREE rewrites are required, not two:

```python
body = body.replace("{Tables/",  "{" + block + "/Tables/")
body = body.replace("{Figures/", "{" + block + "/Figures/")
# bare \@@input (hand-rolled tabulars outside the est* macros have no brace before the path)
body = body.replace("\@@input Tables/", "\@@input " + block + "/Tables/")
```

The third line matters because a block that needs text columns often bypasses `\estwide` and
writes `\begin{tabular*}...\@@input Tables/frag.tex...` directly — the path has no preceding
`{`, so the brace-based rewrite misses it and the master fails with "I can't find file".

## Hoist block-local preamble macros

A block's `results.tex` may define macros between `\input{preamble/preamble}` and
`\begin{document}` (e.g. a bespoke table wrapper). Body-only extraction silently drops them and
the master dies with "Undefined control sequence". Capture that pre-document chunk per block
(minus the `\input{preamble/preamble}` line) and emit it into the master before
`\begin{document}`. Block-named macros avoid cross-block collisions.

## The `\captionof` + `longtable` counter double-advance

Symptom: tables number 1, 3, 5... (standalone or in the master). Cause: a longtable wrapper
(`\estautoLT`) used under `\captionof{table}` advances `\c@table` TWICE — `\captionof` steps it
and the `longtable` environment steps it again on its own. Fix inside the wrapper, so exactly
one mechanism advances the counter:

```latex
\newcommand{\estautoLT}[3]{%
  \edef\est@savedtable{\arabic{table}}%   % save before longtable
  \begin{longtable}{l*{#2}{#3}} ... \end{longtable}%
  \setcounter{table}{\est@savedtable}%    % undo longtable's own step
}
```

Apply the patch in EVERY preamble that compiles the content — the block-local one AND the
master's shared copy (the master never reads block preambles, so a fix living only there
silently vanishes when the body is hoisted).

## Parallel-fixer discipline

When several agents fix different blocks concurrently, exactly one process may own the master
(`make_master.py` + its `pdflatex`) — block fixers rebuild only their own folder, and the
orchestrator reassembles once at the end. Two concurrent master compiles race on
`exhibits.aux`/`.pdf` and produce corrupt or interleaved output.

## Source & license

This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.

- **Author:** [kennethkhoocy](https://github.com/kennethkhoocy)
- **Source:** [kennethkhoocy/applied-micro-skills](https://github.com/kennethkhoocy/applied-micro-skills)
- **License:** MIT

Install and usage instructions live in the source repository linked above.

## Pricing

- **Free** — Free

## Security capabilities

Automated source analysis of v0.1.0 — what this tool can access:

- **Network access:** no
- **Filesystem access:** no
- **Shell / process execution:** no
- **Environment & secrets:** no
- **Dynamic code execution:** no

*"Yes" means the capability is present in the source — more access means more to trust, not that it is unsafe.*


## Versions

- **0.1.0** — security scan: passed — Imported from the upstream source.

## Links

- Listing page: https://agentstack.voostack.com/l/skill-kennethkhoocy-applied-micro-skills-latex-empirical-tables
- Seller: https://agentstack.voostack.com/s/kennethkhoocy
- Browse the marketplace: https://agentstack.voostack.com/browse

---
Listed on AgentStack — the marketplace for AI agent skills and MCP servers. Every listing is security-reviewed. Creators keep 70%.
