4

I am looking for a latex package that can evaluate symbolic expressions by substituting the values of symbols declared in some header. For example, in these set of equations, as you can see, I get certain values of p1, p2 by substituting the values of R, L and C. enter image description here

Here I had to calculate the values of p1,p2 outside of latex software and then manually enter them in (17).

I am looking for a feature where I can declare values of some special variables, R, C and L at the start of the code and then p1, p2 will be automatically evaluated in equation (17) if I use some option telling compiler to substitute the numerical value (I imagine something like : \begin{equation}{evauate = 1}\end{equation}) this will save a lot of time for me as often i make calculation mistakes in these trivial equations and the error propagates in all following equations and lot of unwarranted time wastage happens.

I have heard that in tikz package we can plot functions so I am hoping this has also been possibly implemented.

thanks!

Abby
  • 621
Kutsit
  • 169
  • 1
    You can most probably do this in Lua. – lhf Jun 03 '22 at 17:12
  • 1
    Just a clarification: are you looking for a way to evaluate expressions typeset in LaTeX? Or would you be OK with a solution that simply makes proper calculations inside LaTeX, independently of the typesetting? In the second case, I think that Lua is a better solution that PGF, since it's both more efficient and more precise. – Miyase Jun 03 '22 at 19:19
  • 1
    The numerica package may be of interest to you. https://www.ctan.org/pkg/numerica – LaTeXereXeTaL Jun 03 '22 at 19:47
  • @Miyase presently, I would like to evaluate only expressions typeset in LaTex – Kutsit Jun 05 '22 at 05:03
  • @LaTeXereXeTaL numerica looks very useful, but i installed it, as well as l3kernel and l3packages, mathtools and amsmath however it is not compiling, is something additional needed? as soon i include usepackage{numerica} it, it says missing $ inserted in graphics.sty, did you face something like this – Kutsit Jun 05 '22 at 05:22
  • I get no errors with numerica. I am using an up to date TeX Live 2022. Without seeing your code I have no way of knowing what to tell you next. – LaTeXereXeTaL Jun 05 '22 at 17:35
  • @LaTeXereXeTaL, sorted, I needed to update MikTex, I never opened since i installed it years ago, It is working now – Kutsit Jun 06 '22 at 07:40
  • Excellent! Glad it working for you now. – LaTeXereXeTaL Jun 06 '22 at 17:04

3 Answers3

5

Probably overkill here, but another possibility is to use the sagetex package, which requires installation of SageMath. The source below was processed with pdflatex inside TeXShop, so the directive in the first line was included; in other environments, you would need to run the sage command directly from a command line.

 % !TEX TS-program = sage
\documentclass{article}
\usepackage{amsmath,amsfonts}
\usepackage{sagetex}

\begin{document}

\begin{sageblock} L=1;C=1;R=4; p1 = -R/(2L) + sqrt(R^2/(L^2) - 4/(LC))/2 p2 = -R/(2L) - sqrt(R^2/(L^2) - 4/(LC))/2 \end{sageblock}

\begin{align} p_{1} &= \sage{p1} & p_{2} &= \sage{p2}, \ p_{1} &\approx \sage{n(p1)} & p_{2} &\approx \sage{n(p2)}, \end{align} \end{document}

Using SageMath and sagetex package

murray
  • 7,944
4

You can use pgfmath which is built into tikz:

enter image description here

Code:

\documentclass{article}
\usepackage{pgfmath}% not needed if inckuding `tikz` or `pgfplots`

\newcommand{\ComputeValue}[5][+]{% %% #1 = + | - %% #2 = macro to contain value %% #3 = R %% #4 = L %% #5 = C \pgfmathsetmacro{#2}{% -(#3)/(2 * (#4)) #1 %% Choose + or - (default is + if not specified) sqrt(((#3)/(#4))^2 - 4/((#4)*(#5))) / 2 }% }%

\newcommand{\ValueR}{4} \newcommand{\ValueL}{1} \newcommand{\ValueC}{1}

\begin{document} \ComputeValue{\ValueP}{\ValueR}{\ValueL}{\ValueC} \verb|\ValueP| is \ValueP.

\ComputeValue[-]{\ValueQ}{\ValueR}{\ValueL}{\ValueC} \verb|\ValueQ| is \ValueQ. \end{document}

Peter Grill
  • 223,288
  • Disadvantage is that you have to type the equation twice, once in typesetted LaTeX form and once in pgfmath expression form. I think something like SageTeX (or any proper symbolic engine) will be able to convert the symbolic expression to LaTeX form automatically (although the autogenerated formula might look aesthetically bad at sometimes...) – user202729 Jun 04 '22 at 05:55
2

I felt I should add this as an answer demonstrating the numerica package rather than just a comment, and because I feel it is probably the simplest answer. I note your example contains a complex number and I don't think numerica supports that. I encourage you to contact the developer if necessary. He is active and very open to discussion.

MWE:

% !TEX program = lualatexmk
% !TEX encoding = UTF-8 Unicode

\documentclass{article} \usepackage{numerica}

\begin{document}

\textsf{numerica} has many options so please do read the documentation carefully. Note how \verb|\eval*| suppresses the variable list. Using \verb|xx| forces scientific notation to mantissas in the interval ( [1,10) ); the number sets the number of displayed figures.

\begin{align} p_1 &= \eval{ -\frac{R}{2L} + \frac{\sqrt{R^2/L^2 - 4/LC}}{2} }[R=4,L=1,C=1][10] \ p_2 &= \eval{ -\frac{R}{2L} - \frac{\sqrt{R^2/L^2 - 4/LC}}{2} }[R=4,L=1,C=1][10] \end{align} \begin{align} p_1 &= \eval{ -\frac{R}{2L} + \frac{\sqrt{R^2/L^2 - 4/LC}}{2} }[R=4,L=1,C=1][6xx] \ p_2 &= \eval{ -\frac{R}{2L} - \frac{\sqrt{R^2/L^2 - 4/LC}}{2} }[R=4,L=1,C=1][6xx] \end{align}

You can define and store variables as macros. See the documentation for details.

\NewDocumentCommand{\myR}{}{4} % or \newcommand{\myR}{4} or \def\myR{4} \NewDocumentCommand{\myL}{}{1} % or \newcommand{\myL}{1} or \def\myL{1} \NewDocumentCommand{\myC}{}{1} % or \newcommand{\myC}{1} or \def\myC{1} \macros{\myR,\myL,\myC} \begin{align} p_1 &= \eval{ -\frac{\myR}{2\myL} + \frac{\sqrt{\myR^2/\myL^2 - 4/\myL\myC}}{2} }[6xx] \ p_2 &= \eval{ -\frac{\myR}{2\myL} - \frac{\sqrt{\myR^2/\myL^2 - 4/\myL\myC}}{2} }[6xx] \end{align*}

\end{document}

This is the output.

MWE output