# LaTeX Reference Tool Design
## Overview
The LaTeX reference tool provides instant documentation search for commands, packages, symbols, and error help within TeXFlow.
## Current Implementation
### Basic In-Memory Search
- JSON data files loaded at startup
- Linear search through dictionaries
- Simple substring matching
- ~100 commands, ~50 symbols, 10 error patterns
### Usage Insights
From real document creation, we discovered critical needs:
1. **Command → Package Mapping**: When `\rowcolor` fails, users need to know it requires `colortbl`
2. **Package Availability**: "qtree.sty not found" needs installation guidance
3. **Context-Aware Help**: Error messages should provide specific solutions
## Future Implementation
### Data Architecture
**Primary Sources:**
- **latex2e-help-texinfo**: Core LaTeX commands (GFDL licensed)
- **Comprehensive Symbol List**: 14,000+ symbols (LPPL licensed)
- **CTAN Database**: Package metadata and documentation
**Storage:** SQLite with FTS5 for fast full-text search
```sql
CREATE VIRTUAL TABLE commands_fts USING fts5(
name, description, syntax,
content=commands
);
```
### Search Features
1. **Smart Query Understanding**
- "how to make a table" → table environments
- "symbol for approximately" → ≈ (\\approx)
- "\\frac" → direct command lookup
2. **Context-Aware Ranking**
- Boost results from packages used in current document
- Prioritize based on document type
3. **Package Intelligence**
- Check if package is installed: `kpsewhich package.sty`
- Provide system-specific install commands
- Suggest alternatives for unavailable packages
### Key Implementation Points
```python
class SmartSearch:
def search(self, query, context=None):
# Detect query type (command, symbol, error, how-to)
# Apply fuzzy matching for typos
# Use context to boost relevance
# Return ranked results with metadata
```
### Real-World Features
**Error Pattern Learning:**
```python
error_patterns = {
"Undefined control sequence.*\\\\rowcolor": {
"solution": "Add \\usepackage{colortbl}",
"package": "colortbl"
}
}
```
**Installation Guidance:**
```python
def get_install_command(package):
if has_tlmgr():
return f"tlmgr install {package}"
elif has_miktex():
return f"miktex packages install {package}"
```
## Success Metrics
- **Coverage**: Find 95% of commands in typical documents
- **Speed**: < 50ms search response
- **Accuracy**: Correct package identification 99% of time
- **Helpfulness**: Error solutions that actually work
## Implementation Approach
The tool is designed as a full application that exposes an MCP interface. With proper data sources and SQLite FTS5, the complete implementation is achievable quickly, providing genuine value to LaTeX authors.