在数学模式下自动计算结果

在数学模式下自动计算结果

是否有可用的程序/软件/等,可以让你自动计算并自动完成 TeX 方程式?例如,如果我要计算很长的混乱方程式,比如

120 000 \, inches \times \frac{1 \, mile}{63360 \, inches} =

然后程序就可以用

1.89 \, miles

答案1

以下是基于我的pythontex包,它提供对 SymPy 的访问。我编写了一些代码,它接受用 编写的表达式siunitx,将其转换为 SymPy 格式(如果它不是太复杂并且不会破坏正则表达式),然后计算结果。使用我称为 的宏访问 SymPy 代码\autocalc,该宏接受两个参数:用 编写的表达式siunitx和答案所需的单位(也用 编写siunitx)。如果没有一些额外的正则表达式工作,这不会处理所有事情,但它应该可以处理大多数单行单位转换。

在此处输入图片描述

\documentclass{article}

\usepackage{pythontex}
\usepackage[per-mode=fraction]{siunitx}
% Define some English units
\DeclareSIUnit\inch{in}
\DeclareSIUnit\inches{in}
\DeclareSIUnit\ft{ft}
\DeclareSIUnit\foot{ft}
\DeclareSIUnit\feet{ft}
\DeclareSIUnit\mi{mi}
\DeclareSIUnit\mile{mi}
\DeclareSIUnit\miles{mi}

\begin{sympycode}
from sympy.physics.units import *
import re

def process_units(units):
    ''' Take units in siunitx form, and convert to SymPy format '''
    # Take care of powers
    units = units.replace(r'\squared', '**2')
    units = re.sub(r'\\square(\\[a-z]+)', r'\1**2', units)
    units = units.replace(r'\cubed', '**3')
    units = re.sub(r'\\cubic(\\[a-z]+)', r'\1**3', units)
    # Take care of \per
    units = re.sub(r'\\per(\\[a-z]+\**\d*)', r'/(\1)', units)
    # Add in multiplication as needed
    units = re.sub(r'(\(*\\[a-z]+(?:\*\*\d+)*\)*)(\\[a-z]+)', r'\1*\2', units)
    units = re.sub(r'(\(*\\[a-z]+(?:\*\*\d+)*\)*)(\\[a-z]+)', r'\1*\2', units)
    # Finally, remove backslashes
    units = units.replace('\\', '')
    return units

def process_SI(matchobj):
    ''' Take an \SI macro, and convert it to SymPy form '''
    num, units = matchobj.groups()
    units = process_units(units)
    result = num + '*' + units
    return result

def calc_SI_expression(expr, final_units):
    ''' Turn a series of siunitx expressions into SymPy form, perform
    the calculation, and return the answer in the desired units '''
    # Get rid of spaces in code from TeX
    expr = expr.replace(' ', '')
    final_units = final_units.replace(' ', '')
    # Start assembling the final result
    result = expr + ' = '
    # Take care of multiplication and \left and \right
    expr = expr.replace('^', '**')
    expr = expr.replace(r'\left', '').replace(r'\right', '')
    expr = expr.replace('\\times', '*')
    # Convert all \SI macros to SymPy form
    expr = re.sub(r'\\SI\{(.+?)\}\{(.+?)\}', process_SI, expr)
    expr = re.sub(r'\\frac\{(.+?)\}\{(.+?)\}', r'(\1)/(\2)', expr)
    # Evaluate the string that is now in SymPy form
    expr = eval(expr + '/(' + process_units(final_units) + ')')
    # Round the result to a desired number of places
    expr = round(float(expr), 6)
    # Return original equation plus calculated result
    return result + '\SI{{{0}}}{{{1}}}'.format(expr, final_units)
\end{sympycode}

% Define a shortcut for accessing the Python function
\newcommand{\autocalc}[2]{\sympy{calc_SI_expression(r"#1", r"#2")}}

\begin{document}

\[
\autocalc{
  \SI{120000}{\inches}
  \times
  \frac{\SI{1}{\mile}}{\SI{63360}{\inches}}
}{\miles}
\]

\[
\autocalc{
  \SI{2}{\square\feet\per\second}
  \times
  \left(
  \frac{\SI{12}{\inches}}{\SI{1}{\foot}}
  \right)^2
  \times
  \frac{\SI{3600}{\s}}{\SI{1}{\hour}}
}{\square\inches\per\hour}
\]

\end{document}

答案2

您可以使用 sage (sagetex) 来做到这一点。

%!TEX TS-program = sage
\documentclass{article}
\usepackage{sagetex}
\usepackage{amsmath}
\begin{document}
120000\, inches $\times \frac{1 \text{ mile}}{63360 \text{ inches}}$\,=$\sage{120000*1/63000}$\,miles.
\end{document}

在此处输入图片描述

答案3

以下是可能的方法之一我在评论中指出

\documentclass{article}
\usepackage{expl3,xparse}

\ExplSyntaxOn
% \calculate[<places>]{<fp expr>}
% #1: number of places to round to (optional, default 4)
% #2: floating point expression as described in interface3.pdf
%     section 9, see http://texdoc.net/pkg/interface3
%
% internal command:
\cs_new:Npn \raindrop_calculate:nn #1#2
  { \fp_eval:n { round( #2 , #1 ) } }
% document level:
\NewDocumentCommand \calculate { O{4}m }
  { \raindrop_calculate:nn { #1 } { #2 } }
\ExplSyntaxOff

% formatting of units (probably better: package `siunitx'):
\usepackage{amsmath}
\newcommand*\unit[1]{\text{#1}}

\begin{document}

\[
  120 000\,\unit{inches} \times \frac{ 1\,\unit{mile} }{ 63360\,\unit{inches} }
  =
  \calculate{ 120000 / 63360 }\,\unit{miles}
\]

\end{document}

在此处输入图片描述

答案4

对于简单的计算,您也可以使用 LuaTeX。

以下是 ConTeXt 中的一个示例:

% Define miles as a unit
\registerunit[unit][mile=mile]


\unexpanded\def\InchesToMiles#1%
    {\m{\unit{#1 inch} \times 
          \frac {\unit{1 mile}} {\unit {63360 inch}}
        = \ctxlua{context.unit(string.format("\letterpercent 0.3f mile", #1/63360))}}}


\starttext
\InchesToMiles{120000}
\stoptext

这使

在此处输入图片描述

相关内容