LaTeX Calculator for VSCode
A powerful VSCode extension that calculates LaTeX mathematical expressions directly in Markdown files.
LaTeX 计算器 - VSCode 扩展
一个强大的 VSCode 扩展,可以直接在 Markdown 文件中计算 LaTeX 数学表达式。
English | 中文说明
Features
🧮 Manual Calculation
Press Ctrl+Shift+= (or Cmd+Shift+= on Mac) to calculate LaTeX expressions. The calculator automatically detects the calculation mode:
Selected Mode (when text is selected)
- Select any LaTeX expression in a Markdown file
- If selection ends with "=", result is appended as "result" (without "=")
- Otherwise, result is appended as " = result"
Cursor Mode (when no text is selected)
- Place cursor after a LaTeX expression in math environment
- Calculator finds expression from cursor backwards to comparison operators or math boundary
- Extended Boundaries: Supports
= , > , < , ≤ , ≥ , ≠ , \leq , \geq , \neq , \ne
- If cursor is after "=", result is appended as "result" (without "=")
- If cursor is not after "=", result is appended as " = result"
1
🔄 Auto Calculation
- Tracking Mode: Automatically calculates when you type
= in math environments
- Global Mode: Scans and calculates all expressions in the document
- Smart Boundary Detection: Uses comparison operators (
> , < , ≤ , ≥ , ≠ , \leq , \geq , \neq , \ne ) as expression boundaries
- Unit Support: Handles expressions like
$1+2= kPa$ (units after equals sign)
- HTML Comments Compatible: Works correctly even with
<!-- --> comments in the document
- Toggle auto calculation with the status bar button
Note: Only = triggers auto calculation; other operators serve as boundaries for better expression parsing.
🔢 Enhanced Math Support
Traditional Trigonometric Notation
- Power Notation: Use
\tan^2 60° instead of (\tan 60°)^2
- Complex Expressions: Support for
\tan^2 {(30°+30°)} and \tan^2 (30°+30°)
- All Functions: Works with sin, cos, tan, cot, sec, csc and inverse functions
- Nested Expressions: Intelligent parsing of complex mathematical expressions
- Persistent Python Daemon: Uses a long-running Python process for ultra-fast calculations
- 93% Performance Improvement: Average calculation time reduced from ~618ms to ~43ms
- Real-time Auto Calculation: Near-instantaneous response in auto calculation mode
📚 Calculation History
- Automatic Recording: Every calculation (successful or failed) is automatically saved to history
- Customizable Limit: Set the maximum number of history entries (1-1000, default: 20)
- Visual Indicators: Success calculations show green border, errors show red border
- Quick Actions:
- Click to re-insert expressions into the current document
- Delete individual history items
- Clear all history with one click
- Persistent Storage: History is saved across VSCode sessions
- Chronological Order: Most recent calculations appear at the top
- Smart Fallback: Automatically falls back to traditional mode if daemon fails
- Configurable: Can disable daemon mode in settings if needed
⚙️ Configurable Settings
- Decimal Places: Control precision (0-10 decimal places)
- Scientific Notation: Toggle scientific notation format
- Show Formula: Display complete formula with result
- Degree/Radian Mode: Choose angle units for trigonometric functions
- Daemon Mode: Enable/disable persistent Python process for better performance
- Display Mode: Control LaTeX formatting (no symbols, single $, double $$)
📝 Math Environment Support
- Inline math:
$expression = result$
- Block math:
$$expression = result$$
- Automatic detection and parsing of nested expressions
Installation
Prerequisites
- Python 3.6+ must be installed and accessible from command line
- Required Python packages (auto-installed by extension):
Install Extension
- Open VSCode
- Go to Extensions (Ctrl+Shift+X)
- Search for "LaTeX Calculator"
- Click Install
Usage
Manual Calculation
- Open a Markdown file (.md)
- Select a LaTeX expression (e.g.,
\frac{1}{2} + \sin{30^\circ} )
- Press
Ctrl+Shift+= (Windows/Linux) or Cmd+Shift+= (Mac)
- The result will be appended:
\frac{1}{2} + \sin{30^\circ} = 1
Auto Calculation
Enable auto calculation by clicking the calculator icon in the status bar
Type LaTeX expressions in math environments:
$2 + 3 = $ → $2 + 3 = 5$
$$\sqrt{16} = $$ → $$\sqrt{16} = 4$$
Supported LaTeX Functions
- Basic Operations:
+ , - , * , / , ^
- Trigonometric:
\sin , \cos , \tan , \cot , \sec , \csc
- Inverse Trigonometric:
\arcsin , \arccos , \arctan , \asin , \acos , \atan
- Mathematical:
\sqrt , \frac , \log , \ln , \exp
- Constants:
\pi , \e
- Operators:
\times , \div , \cdot
Configuration
Access settings via File > Preferences > Settings and search for "LaTeX Calculator":
Setting |
Default |
Description |
latexCalculator.decimalPlaces |
2 |
Number of decimal places in results |
latexCalculator.scientificNotation |
false |
Use scientific notation |
latexCalculator.showFormula |
false |
Show complete formula with result |
latexCalculator.degreeMode |
true |
Use degrees for trigonometric functions |
latexCalculator.displayMode |
0 |
Formula display mode (0=none, 1=single $, 2=double $$) |
latexCalculator.autoCalculate |
false |
Enable automatic calculation |
latexCalculator.pythonPath |
"python" |
Path to Python executable |
latexCalculator.resultTransferToDegree |
false |
Convert calculation results to degrees |
Commands
Command |
Shortcut |
Description |
LaTeX Calculator: Calculate Expression |
Ctrl+Shift+= |
Calculate selected expression |
LaTeX Calculator: Toggle Auto Calculate |
- |
Toggle automatic calculation |
LaTeX Calculator: Calculate All Expressions |
- |
Calculate all expressions in document |
Examples
Basic Arithmetic
$2 + 3 \times 4 = $ → $2 + 3 \times 4 = 14$
Trigonometry
$\sin{30^\circ} + \cos{60^\circ} = $ → $\sin{30^\circ} + \cos{60^\circ} = 1$
Enhanced Trigonometric Notation
$\tan^2 60° = $ → $\tan^2 60° = 3$
$\tan^2 {(30°+30°)} = $ → $\tan^2 {(30°+30°)} = 3$
Complex Expressions
$$\frac{\sqrt{16} + \log{100}}{2} = $$ → $$\frac{\sqrt{16} + \log{100}}{2} = 3$$
$$\tan 60° + \sqrt{\frac{(\tan^2 60° + 1)(\tan 60° - \tan 0°)}{\tan 60° - \tan 48°}} = $$ → $$\tan 60° + \sqrt{\frac{(\tan^2 60° + 1)(\tan 60° - \tan 0°)}{\tan 60° - \tan 48°}} = 5.07101$$
Smart Boundary Detection and Units
$>1+2= $ → $>1+2= 3$ (auto calculates at =)
$<3+4= kPa$ → $<3+4= 7 kPa$ (auto calculates at =, preserves unit)
$5+6\geq$ → Manual calculation only (no auto trigger)
Note: Auto calculation only triggers at = . Other operators serve as smart boundaries for expression parsing.
Inverse Trigonometric
$\arcsin{0.5} = $ → $\arcsin{0.5} = 30$ (in degree mode)
Troubleshooting
Python Environment Issues
If you see "Python environment not ready" warnings:
- Install Python: Download from python.org
- Check Python Path: Ensure
python command works in terminal
- Configure Path: Set
latexCalculator.pythonPath in settings if needed
- Install Packages: Extension will prompt to install required packages
Calculation Errors
- Invalid Expression: Check LaTeX syntax (balanced braces, proper commands)
- Domain Errors: Ensure function inputs are within valid ranges
- Timeout: Complex calculations timeout after 5 seconds
- Large Documents: Auto calculation only processes visible areas
- Complex Expressions: Consider breaking into simpler parts
- Disable Auto Mode: Turn off auto calculation for better performance
Contributing
Found a bug or want to contribute? Visit our GitHub repository.
License
This extension is licensed under the MIT License.
Changelog
1.1.0
- Enhanced Trigonometric Support: Traditional notation like
\tan^2 60°
- Extended Auto Mode: Support for comparison operators (>, <, ≤, ≥, ≠)
- Unit Support: Auto calculation with units like
$1+2= kPa$
- Unified Manual Mode: Smart detection between selected and cursor modes
- HTML Comment Compatibility: Works with
<!-- --> comments
- Complex Expression Parsing: Improved handling of nested expressions
1.0.0
- Initial release
- Manual and automatic calculation modes
- Comprehensive LaTeX function support
- Configurable settings and display options
中文说明
🎯 功能概述
LaTeX Calculator 是一个专为 VSCode 设计的强大数学计算扩展,能够直接在 Markdown 文件中计算 LaTeX 数学表达式。无论是简单的算术运算还是复杂的三角函数计算,都能轻松处理。
✨ 核心功能
🧮 手动计算模式
使用快捷键 Ctrl+Shift+= (Mac: Cmd+Shift+= )进行计算,支持两种智能检测模式:
选中模式(选择文本时)
- 选择任意 LaTeX 表达式
- 如果选择内容以 "=" 结尾,结果直接追加为 "结果"(不含 "=")
- 否则结果追加为 " = 结果"
光标模式(未选择文本时)
- 将光标放在 LaTeX 表达式后
- 自动从光标位置向前查找表达式,直到比较运算符或数学环境边界
- 扩展边界支持:
= , > , < , ≤ , ≥ , ≠ , \leq , \geq , \neq , \ne
- 智能判断是否需要添加等号
🔄 自动计算模式
- 跟踪模式:在数学环境中输入
= 时自动触发计算
- 全局模式:扫描并计算文档中的所有表达式
- 智能边界检测:使用比较运算符作为表达式边界
- 单位支持:处理如
$1+2= kPa$ 这样带单位的表达式
- HTML 注释兼容:即使文档中有
<!-- --> 注释也能正常工作
- 通过状态栏按钮切换自动计算
注意:只有 = 会触发自动计算,其他运算符仅作为边界用于更好的表达式解析。
🔍 调试模式
- 实时计算:编辑时实时更新表达式计算结果
- 智能响应:只在有实际内容变化时触发计算
- 性能优化:避免在大段文本选择时响应
- 互斥模式:与自动计算模式互斥,确保不冲突
🔢 增强数学支持
传统三角函数记法
- 幂次记法:使用
\tan^2 60° 而不是 (\tan 60°)^2
- 复杂表达式:支持
\tan^2 {(30°+30°)} 和 \tan^2 (30°+30°)
- 全函数支持:适用于 sin, cos, tan, cot, sec, csc 及反函数
- 嵌套表达式:智能解析复杂数学表达式
⚡ 高性能计算
- 持久化 Python 守护进程:使用长期运行的 Python 进程实现超快计算
- 93% 性能提升:平均计算时间从 ~618ms 降至 ~43ms
- 实时自动计算:自动计算模式下近乎瞬时响应
- 智能降级:守护进程失败时自动回退到传统模式
📚 计算历史记录
- 自动记录:每次计算(成功或失败)都自动保存到历史记录
- 可定制限制:设置历史记录最大条数(1-1000,默认:20)
- 视觉指示器:成功计算显示绿色边框,错误显示红色边框
- 快捷操作:
- 点击重新插入表达式到当前文档
- 删除单个历史记录项
- 一键清空所有历史记录
- 持久化存储:历史记录在 VSCode 会话间保持
- 时间顺序:最新计算显示在顶部
⚙️ 丰富的配置选项
计算设置
- 小数位数:控制精度(0-10 位小数)
- 科学记数法:切换科学记数法格式
- 显示完整公式:在结果中显示完整公式
- 度数/弧度模式:选择三角函数的角度单位
- 结果转换为度数:将计算结果转换为度数显示
显示设置
- 显示模式:控制 LaTeX 格式(无符号、单 $、双 $$)
- 公式显示:选择是否显示原始表达式
高级设置
- 守护进程模式:启用/禁用持久化 Python 进程
- Python 路径:自定义 Python 可执行文件路径
- 自定义结束标识符:添加自定义的表达式分隔符
- 历史记录条数:设置历史记录保存的最大数量
📝 数学环境支持
- 行内数学:
$表达式 = 结果$
- 块级数学:
$$表达式 = 结果$$
- 自动检测:自动检测和解析嵌套表达式
🚀 安装指南
系统要求
- Python 3.6+ 必须已安装并可从命令行访问
- 必需的 Python 包(扩展会自动安装):
安装扩展
- 打开 VSCode
- 转到扩展面板(Ctrl+Shift+X)
- 搜索 "LaTeX Calculator"
- 点击安装
📖 使用方法
手动计算
- 打开 Markdown 文件(.md)
- 选择 LaTeX 表达式(例如:
\frac{1}{2} + \sin{30^\circ} )
- 按
Ctrl+Shift+= (Windows/Linux)或 Cmd+Shift+= (Mac)
- 结果将被追加:
\frac{1}{2} + \sin{30^\circ} = 1
自动计算
通过点击状态栏中的计算器图标启用自动计算
在数学环境中输入 LaTeX 表达式:
$2 + 3 = $ → $2 + 3 = 5$
$$\sqrt{16} = $$ → $$\sqrt{16} = 4$$
调试模式
- 在侧边栏设置面板中启用调试模式
- 在数学环境中编辑表达式时,结果会实时更新
🔧 支持的 LaTeX 函数
- 基本运算:
+ , - , * , / , ^
- 三角函数:
\sin , \cos , \tan , \cot , \sec , \csc
- 反三角函数:
\arcsin , \arccos , \arctan , \asin , \acos , \atan
- 数学函数:
\sqrt , \frac , \log , \ln , \exp
- 常数:
\pi , \e
- 运算符:
\times , \div , \cdot
⚙️ 配置说明
通过 文件 > 首选项 > 设置 访问设置,搜索 "LaTeX Calculator":
设置项 |
默认值 |
说明 |
latexCalculator.decimalPlaces |
2 |
结果中的小数位数 |
latexCalculator.scientificNotation |
false |
使用科学记数法 |
latexCalculator.showFormula |
false |
显示完整公式和结果 |
latexCalculator.degreeMode |
true |
三角函数使用度数 |
latexCalculator.displayMode |
0 |
公式显示模式(0=无,1=单$,2=双$$) |
latexCalculator.autoCalculate |
false |
启用自动计算 |
latexCalculator.debugMode |
false |
启用调试模式 |
latexCalculator.pythonPath |
"python" |
Python 可执行文件路径 |
latexCalculator.resultTransferToDegree |
false |
将计算结果转换为度数 |
latexCalculator.useDaemonMode |
true |
使用守护进程模式提升性能 |
latexCalculator.customEndIdentifiers |
[",", ";"] |
自定义结束标识符 |
latexCalculator.historyLimit |
20 |
历史记录最大条数 |
🎮 命令列表
命令 |
快捷键 |
说明 |
LaTeX Calculator: Calculate Expression |
Ctrl+Shift+= |
计算选中的表达式 |
LaTeX Calculator: Toggle Auto Calculate |
- |
切换自动计算模式 |
LaTeX Calculator: Toggle Debug Mode |
- |
切换调试模式 |
LaTeX Calculator: Calculate All Expressions |
- |
计算文档中的所有表达式 |
LaTeX Calculator: Open Settings |
- |
打开设置面板 |
💡 使用示例
基础算术
$2 + 3 \times 4 = $ → $2 + 3 \times 4 = 14$
三角函数
$\sin{30^\circ} + \cos{60^\circ} = $ → $\sin{30^\circ} + \cos{60^\circ} = 1$
增强三角函数记法
$\tan^2 60° = $ → $\tan^2 60° = 3$
$\tan^2 {(30°+30°)} = $ → $\tan^2 {(30°+30°)} = 3$
复杂表达式
$$\frac{\sqrt{16} + \log{100}}{2} = $$ → $$\frac{\sqrt{16} + \log{100}}{2} = 3$$
$$\tan 60° + \sqrt{\frac{(\tan^2 60° + 1)(\tan 60° - \tan 0°)}{\tan 60° - \tan 48°}} = $$ → $$\tan 60° + \sqrt{\frac{(\tan^2 60° + 1)(\tan 60° - \tan 0°)}{\tan 60° - \tan 48°}} = 5.07101$$
智能边界检测和单位
$>1+2= $ → $>1+2= 3$ (在 = 处自动计算)
$<3+4= kPa$ → $<3+4= 7 kPa$ (在 = 处自动计算,保留单位)
$5+6\geq$ → 仅手动计算(无自动触发)
注意:自动计算只在 = 处触发。其他运算符作为表达式解析的智能边界。
反三角函数
$\arcsin{0.5} = $ → $\arcsin{0.5} = 30$ (度数模式下)
🔧 故障排除
Python 环境问题
如果看到 "Python environment not ready" 警告:
- 安装 Python:从 python.org 下载
- 检查 Python 路径:确保终端中
python 命令有效
- 配置路径:如需要,在设置中设置
latexCalculator.pythonPath
- 安装包:扩展会提示安装所需包
计算错误
- 无效表达式:检查 LaTeX 语法(平衡的大括号、正确的命令)
- 域错误:确保函数输入在有效范围内
- 超时:复杂计算在 5 秒后超时
性能问题
- 大文档:自动计算只处理可见区域
- 复杂表达式:考虑分解为更简单的部分
- 禁用自动模式:关闭自动计算以获得更好性能
🎨 界面特性
- 现代化侧边栏:美观的设置面板,支持实时配置
- 状态指示器:直观的开关状态显示
- 历史记录面板:完整的计算历史管理
- 智能提示:详细的功能说明和使用提示
🤝 贡献
发现 bug 或想要贡献?访问我们的 GitHub 仓库。
📄 许可证
此扩展基于 MIT 许可证。
📝 更新日志
1.2.12(最新版本)
- 增强界面质感:全新的按钮和交互效果
- 历史记录功能:完整的计算历史管理
- 调试模式:实时表达式计算
- 守护进程模式:显著提升计算性能
- 自定义结束标识符:灵活的表达式分隔配置
1.1.0
- 增强三角函数支持:传统记法如
\tan^2 60°
- 扩展自动模式:支持比较运算符(>, <, ≤, ≥, ≠)
- 单位支持:自动计算支持单位如
$1+2= kPa$
- 统一手动模式:选中和光标模式的智能检测
- HTML 注释兼容:支持
<!-- --> 注释
- 复杂表达式解析:改进嵌套表达式处理
1.0.0
- 初始发布
- 手动和自动计算模式
- 全面的 LaTeX 函数支持
- 可配置的设置和显示选项
| |