0

TeX is not really good in calculating, so some packages and LuaTeX were developed. But none of these solutions offers symbolic/exact mathematics, so I have tried something like this:

% Plain TeX
\catcode`\@=11

% add 2 rational numbers, expandable macro
\def\@rational@add#1#2#3#4{%
    \ifnum\numexpr#2*#4 < \z@
      \expandafter\the\expandafter
        \numexpr\expanded{%
            (-#1*#4-#2*#3)/\expandafter\@gcd\expanded{%
                {\the\numexpr#1*#4+#2*#3\relax}{\the\numexpr#2*#4\relax}}}\relax
      /
      \expandafter\the\expandafter
        \numexpr\expanded{%
            (-#2*#4)/\expandafter\@gcd\expanded{%
                {\the\numexpr#1*#4+#2*#3\relax}{\the\numexpr#2*#4\relax}}}\relax
    \else
      \expandafter\the\expandafter
        \numexpr\expanded{%
            (#1*#4+#2*#3)/\expandafter\@gcd\expanded{%
                {\the\numexpr#1*#4+#2*#3\relax}{\the\numexpr#2*#4\relax}}}\relax
      /
      \expandafter\the\expandafter
        \numexpr\expanded{%
            (#2*#4)/\expandafter\@gcd\expanded{%
                {\the\numexpr#1*#4+#2*#3\relax}{\the\numexpr#2*#4\relax}}}\relax
    \fi}

% helper macros:
\def\@sgn@of@int#1{%
    \ifnum #1 = \z@
      0%
    \else
      \ifnum #1 > \z@ 1 \else -1 \fi
    \fi}
\def\@abs@of@int#1{\ifnum #1 < \z@ \the\numexpr-#1\relax \else \the\numexpr#1 \fi}
\def\@gcd#1#2{%
    \ifnum #2 = \z@
      \@abs@of@int{#1}%
    \else
      \expandafter\@gcd\expanded{{#2}{\the\numexpr #1-#2*(#1/#2)\relax}}
    \fi}

% example
$$ \@rational@add{89}{-24}{-55}{36} $$ prints -377/72 as expected

\bye

But writing \@rational@add and similar macros all the time ... :( Some packages are able to parse somthing like 3/4 + 1/3 * ... and transform it into the right macros. But I don't understand how they do it. Can someone help me?

My dream would be writing somthing like \mathexpr 8^(1/2) + 32^(1/2) which typesets 6 \sqrt2. I don't know if this is possible in TeX or LuaTeX.

12
2

As the comments above indicate, the sagetex package is worth looking into.

\documentclass{article}
\usepackage{sagetex,amsmath}
\begin{document}
Is your dream to write:
$8^{1/2} + 32^{1/2}= \sage{8^(1/2)+32^(1/2)}$
or $\sage{8^(1/2)}+\sage{32^(1/2)}=\sage{8^(1/2)+32^(1/2)}$?
\end{document}

Gives the output: enter image description here

No difficult coding required. The sagetex package uses the computer algebra system SAGE which is not part of your LaTeX installation. That means either SAGE should be installed on your machine or you access it using a free Cocalc account. Since installation of SAGE can be tricky for some computers, I'd recommend opening a Cocalc account. Search this site for numerous posts showing what can be done.

4
  • How is CoCalc relevant here? – fractal Apr 27 '20 at 14:09
  • I've edited my explanation to answer your question. – DJP Apr 27 '20 at 14:15
  • Thanks. I will have a look on this package. Maybe it is possible to adapt it to Plain TeX. – Weißer Kater Apr 27 '20 at 14:41
  • From here, "You can choose between PDF Latex, XeLaTeX or LuaTeX." and the link for LuaTex says, "LuaTeX is an extended version of pdfTeX using Lua as an embedded scripting language". – DJP Apr 27 '20 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.