How to Output Query Results as LaTeX Tables in the DuckDB CLI

This article demonstrates how to output your DuckDB query results in LaTeX table format, which can be useful when preparing academic papers, technical documents, or any content that needs to be typeset using LaTeX.

Checking the Current Mode

First, let’s check the current output mode. Here are two ways to do this:

Using the .mode command:

.mode

Sample output:

current output mode: json

My current output mode is json.

We can also run a sample query to see the output format:

CREATE TABLE experiments(id INTEGER, variable VARCHAR, result DECIMAL, significance DECIMAL);
INSERT INTO experiments VALUES 
    (1, 'Control', 0.856, 0.001),
    (2, 'Treatment A', 0.923, 0.005);
SELECT * FROM experiments;

Output:

[{"id":1,"variable":"Control","result":0.856,"significance":0.001},
{"id":2,"variable":"Treatment A","result":0.923,"significance":0.005}]

The result of the query is returned as a JSON document.

Changing to LaTeX Mode

To switch to LaTeX output mode, use:

.mode latex

Verifying the Change

Check the mode setting:

.mode

This will confirm that LaTeX mode is active:

current output mode: latex

The output mode is now latex as expected.

Run a query to see the LaTeX table output:

SELECT * FROM experiments;

The output is now formatted as a LaTeX table:

\begin{tabular}{|rlrr|}
\hline
id & variable & result & significance \\
\hline
1 & Control & 0.856 & 0.001 \\
2 & Treatment A & 0.923 & 0.005 \\
\hline
\end{tabular}

When compiled, this creates a properly formatted table. The output includes:

  • The tabular environment
  • Column alignment specifications
  • Column headers
  • A horizontal line (\hline)
  • Data rows with cell separators (&)
  • Row terminators (\\)

Reverting to the Previous Mode

To return to the previous output mode:

.mode json

That switches to json mode, but you could switch to any mode you like, or keep it in latex mode.

Listing All Available Output Modes

To see all available output modes in the DuckDB CLI:

.help mode

Output:

Output:

.mode MODE ?TABLE?       Set output mode
MODE is one of:
ascii Columns/rows delimited by 0x1F and 0x1E
box Tables using unicode box-drawing characters
csv Comma-separated values
column Output in columns. (See .width)
duckbox Tables with extensive features
html HTML <table> code
insert SQL insert statements for TABLE
json Results in a JSON array
jsonlines Results in a NDJSON
latex LaTeX tabular environment code
line One value per line
list Values delimited by "|"
markdown Markdown table format
quote Escape answers as for SQL
table ASCII-art table
tabs Tab-separated values
tcl TCL list elements
trash No output

Each mode has its own use cases, for example:

  • html for web integration
  • csv for spreadsheet compatibility
  • json for API responses
  • markdown for documentation
  • insert for generating SQL INSERT statements
  • ndjson for streaming data processing

The LaTeX mode is particularly useful when:

  • Writing academic papers
  • Preparing technical documentation
  • Creating scientific reports
  • Generating tables for academic theses
  • Making presentation slides with LaTeX beamer
  • Publishing in academic journals that require LaTeX format