Skip to content

LaTeX Setup for Equation Rendering

This guide explains how to set up LaTeX support for rendering mathematical equations in PDF reports generated by the Nexus Research Agent.

Why LaTeX?

Scientific research reports often contain mathematical equations, formulas, and technical symbols. LaTeX is the gold standard for typesetting mathematics and ensures publication-quality equation rendering in PDFs.

Overview

The Nexus Research Agent uses an intelligent format decision system:

  1. Analyzes the topic to detect if equations are needed
  2. Checks LLM capabilities for direct PDF generation
  3. Chooses optimal format:
  4. LaTeX → For math-heavy topics (Physics, Math, ML theory, etc.)
  5. Markdown → For general research (Business, Social sciences, etc.)
  6. Direct PDF → If LLM supports it (future capability)

When LaTeX is chosen, the system: - Generates complete LaTeX source code - Compiles it with XeLaTeX - Produces a professional PDF with perfect equation rendering

Installation

macOS

BasicTeX is a minimal TeX distribution perfect for most research reports.

# Download BasicTeX
curl -L -o /tmp/BasicTeX.pkg https://mirror.ctan.org/systems/mac/mactex/BasicTeX.pkg

# Install (requires sudo)
sudo installer -pkg /tmp/BasicTeX.pkg -target /

# Add to PATH
echo 'export PATH="/usr/local/texlive/2025basic/bin/universal-darwin:$PATH"' >> ~/.zshrc
source ~/.zshrc

# Verify installation
xelatex --version

Expected output:

XeTeX 3.141592653-2.6-0.999996 (TeX Live 2025)

Option 2: Full MacTeX (7GB - if you need extensive LaTeX packages)

# Download from https://www.tug.org/mactex/
# Or use Homebrew
brew install --cask mactex

# Add to PATH
echo 'export PATH="/Library/TeX/texbin:$PATH"' >> ~/.zshrc
source ~/.zshrc

Linux (Ubuntu/Debian)

# Install TeX Live
sudo apt-get update
sudo apt-get install texlive-xetex texlive-latex-extra

# Verify
xelatex --version

Linux (Fedora/RHEL)

# Install TeX Live
sudo dnf install texlive-xetex texlive-collection-latexextra

# Verify
xelatex --version

Windows

  1. Download MiKTeX from https://miktex.org/download
  2. Run the installer
  3. During installation, choose "Install missing packages automatically"
  4. Add MiKTeX bin directory to PATH:
  5. Usually: C:\Program Files\MiKTeX\miktex\bin\x64\
  6. Verify in PowerShell:
    xelatex --version
    

Verification

Test that LaTeX compilation works:

# Create a test LaTeX file
cat > /tmp/test.tex << 'EOF'
\documentclass{article}
\usepackage{amsmath}
\begin{document}
Test equation: $E = mc^2$

Display equation:
\[
\int_{-\infty}^{\infty} e^{-x^2} dx = \sqrt{\pi}
\]
\end{document}
EOF

# Compile it
cd /tmp
xelatex test.tex

# Check if PDF was created
ls -lh test.pdf

If test.pdf exists, LaTeX is working correctly!

Troubleshooting

"xelatex: command not found"

Problem: LaTeX is not in your PATH.

Solution (macOS):

# Find where XeLaTeX is installed
find /usr/local/texlive -name xelatex 2>/dev/null

# Add the directory to PATH
export PATH="/usr/local/texlive/2025basic/bin/universal-darwin:$PATH"

# Make it permanent
echo 'export PATH="/usr/local/texlive/2025basic/bin/universal-darwin:$PATH"' >> ~/.zshrc

Solution (Linux):

# Usually in /usr/bin
which xelatex

# If not found, reinstall
sudo apt-get install --reinstall texlive-xetex

"! LaTeX Error: File `amsmath.sty' not found"

Problem: Missing LaTeX packages.

Solution (BasicTeX on macOS):

# Install package manager
sudo tlmgr update --self

# Install missing packages
sudo tlmgr install amsmath amssymb amsfonts

Solution (Linux):

# Install extra packages
sudo apt-get install texlive-latex-extra

Compilation fails with "! Emergency stop"

Problem: Syntax error in LaTeX code.

Solution: 1. Check the .log file for specific error 2. The error usually indicates the line number 3. Common issues: - Unescaped special characters: _, %, &, # - Mismatched braces: { without } - Missing packages in preamble

PDF not generated after compilation

Problem: Compilation completed but no PDF.

Solution:

# Check for errors in the log
cat document.log | grep "Error"

# Try compiling twice (for references)
xelatex document.tex
xelatex document.tex

Cross-Platform Compatibility

Nexus automatically detects LaTeX installations across different platforms:

Automatic Path Detection

The system searches for XeLaTeX in these locations:

macOS: - /usr/local/texlive/*/bin/* (BasicTeX, TeX Live - all versions) - /Library/TeX/texbin (MacTeX) - /usr/local/bin (Homebrew)

Linux: - /usr/bin (Standard package manager installations) - /usr/local/texlive/bin

Windows (WSL): - /mnt/c/Program Files/MiKTeX/miktex/bin/x64 - /mnt/c/texlive/bin/win32

No configuration needed - the system will find your LaTeX installation automatically!

If XeLaTeX is in your system PATH, it will work immediately. If not, the system tries common installation locations.

How Nexus Uses LaTeX

1. Format Decision

When you request a research report, Nexus analyzes the topic:

# Example: Physics topic
topic = "Physics-Informed Neural Networks for PDEs"

# System detects: equations needed → chooses LaTeX format
format_decision = {
    "format": "latex",
    "reasoning": "Topic involves physics and PDEs, equations needed",
    "needs_equations": True
}

2. LaTeX Generation

The Writer agent generates complete LaTeX source:

\documentclass[11pt,article]{article}
\usepackage{amsmath,amssymb,amsfonts}
\usepackage{graphicx}
\usepackage{hyperref}

\title{Physics-Informed Neural Networks for Partial Differential Equations}
\author{Nexus Research Agent}
\date{\today}

\begin{document}
\maketitle

\section{Introduction}
Physics-Informed Neural Networks (PINNs) combine...

The loss function is defined as:
\[
\mathcal{L} = \mathcal{L}_{\text{data}} + \lambda \mathcal{L}_{\text{PDE}}
\]

\end{document}

3. Compilation

Nexus compiles the LaTeX to PDF:

from nexus.agents.research.pdf_utils import latex_to_pdf

success, error = latex_to_pdf(
    latex_content=latex_source,
    output_path=Path("output/report.pdf")
)

4. Result

You get a professional PDF with beautifully rendered equations!

Package Requirements

The system automatically includes these LaTeX packages:

  • amsmath - Advanced math typesetting
  • amssymb - Math symbols
  • amsfonts - Math fonts
  • graphicx - Graphics support
  • hyperref - Hyperlinks and PDF metadata
  • geometry - Page layout

If your reports need additional packages, they can be added to the LaTeX template in format_decision.py.

Performance

  • BasicTeX installation: ~2 minutes
  • First LaTeX compilation: ~5-10 seconds (package loading)
  • Subsequent compilations: ~2-3 seconds
  • Disk space: 116MB (BasicTeX) to 7GB (Full MacTeX)

Best Practices

  1. Use BasicTeX unless you need specialized packages
  2. Keep TeX Live updated: sudo tlmgr update --all
  3. Check PATH if commands aren't found
  4. Run compilation twice for references and citations
  5. Check logs if PDF isn't generated

Alternative: Markdown for Non-Technical Reports

If your research doesn't need equations, Nexus automatically uses Markdown:

# Example: Social science topic
nexus-research "Impact of Social Media on Society" --pdf

# System detects: no equations needed → uses Markdown
# Faster generation, smaller dependencies

Support

If you encounter issues:

  1. Check this guide's Troubleshooting section
  2. Verify LaTeX installation: xelatex --version
  3. Test with the verification example above
  4. Check the Nexus logs for specific errors

References