2

Although wolframscript is fully working on the command prompt of Windows 10, the package latexalpha2 gives this error

Package latexalpha2 Error: WolframScript cannot be found, which is required to use latexalpha2.

Inspired by the comments on this relevant question, the package might be designed to work on Linux devices only.

So, I hope if someone can find the required edits to the following package file to make it work with lualatex on Windows 10.

%% This is file `latexalpha2.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% latexalpha2.dtx  (with options: `package')
%% 
%% This is a generated file.
%% 
%% Copyright (C) 2019 by Yi Liu <me@yliu.io>
%% 
%% This file may be distributed and/or modified under the conditions of
%% the LaTeX Project Public License, either version 1.3c of this license
%% or (at your option) any later version. The latest version of this
%% license is in:
%% 
%%    http://www.latex-project.org/lppl.txt
%% 
%% and version 1.3c or later is part of all distributions of LaTeX
%% version 2006/05/20 or later.
%% 
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{latexalpha2}
  [2019/03/05 v1.1 latexalpha2]
\RequirePackage{graphicx}
\RequirePackage{amsmath}
\RequirePackage{etoolbox}
\RequirePackage{pdftexcmds}
\RequirePackage{morewrites}
\RequirePackage{ifxetex}

\newbool{ifcache} \newbool{ifmathics}

\DeclareOption{local}{\def\la@platform{-local}} \DeclareOption{cloud}{\def\la@platform{-cloud}} \DeclareOption{cache}{\booltrue{ifcache}} \DeclareOption{nocache}{\boolfalse{ifcache}} \DeclareOption{mathics}{\booltrue{ifmathics}}

\ExecuteOptions{local,cache} \ProcessOptions\relax

\begingroup \makeatletter \catcode#=12 \catcode&=12 \gdef@hashchar{#} \gdef@ampchar{&} \gdef\backslash{@backslashchar} \gdef\hash{@hashchar} \makeatother \endgroup

\def\la@codetempfile{latexalpha2_code.tmp} \def\la@resulttempfile{} \def\la@currcodehash{}

\newcommand{\la@cleancodetempfile}{\immediate\write18{rm -f \la@codetempfile}} \newcommand{\la@unknownoption}[2]{\PackageError{latexalpha2}{Unknown option '#2' for @backslashchar #1}{}}

\newcommand{\la@executewolfram}[1]{% \IfFileExists{\la@resulttempfile}{% \ifbool{ifcache}{\message{found cached output file \la@resulttempfile^^J}}{\la@executewolframcore{#1}}% }{\la@executewolframcore{#1}}% }

\newcommand{\la@executewolframcore}[1]{% \notbool{ifmathics}{% \def\la@wolframscript{wolframscript -file \la@codetempfile\space -print \ifblank{#1}{}{-format #1} \la@platform\space> \la@resulttempfile}% }{% \def\la@wolframscript{mathics \la@codetempfile\space> \la@resulttempfile}% }% \message{command: \la@wolframscript^^J}% \immediate\write18{\la@wolframscript}% }

\newcommand{\la@writecodefile}[2]{% \message{------------ latexalpha2 -------------^^J}% \message{\notbool{ifmathics}{wolfram}{mathics} code: #1^^J}% \ifblank{#2}{}{\message{option: #2}}% \gdef\la@currcodehash{\ifxetex\mdfivesum{#1#2}\else\pdfmdfivesum{#1#2}\fi}% \gdef\la@resulttempfile{.latexalpha2_\la@currcodehash.out}% \newwrite\tempfile% \immediate\openout\tempfile=\la@codetempfile% \immediate\write\tempfile{#1}% \immediate\closeout\tempfile% }

\newcommand{\la@notformathics}[1]{% \ifbool{ifmathics}{\PackageError{latexalpha2}{@backslashchar #1 is not available for the Mathics mode}{}}{}% }

\def\la@texformat{tex} \def\la@textformat{text} \def\la@wolframformat{wolfram} \def\la@wolframtwoformat{wolfram2} \def\la@pdfformat{pdf} \def\la@pngformat{png} \def\la@jpgformat{jpg}

\newcommand{\wolfram}[2][tex]{% \def\la@currformat{#1}% \la@cleancodetempfile% \notbool{ifmathics}{% \la@writecodefile{#2}{#1}% \ifx \la@currformat \la@texformat \la@executewolfram{TeXForm} \else \ifx \la@currformat \la@textformat \la@executewolfram{Text} \else \ifx \la@currformat \la@wolframformat \la@executewolfram{} \else \la@unknownoption{wolfram}{#1} \fi\fi\fi }{% \ifx \la@currformat \la@texformat \la@writecodefile{Print[TeXForm[#2]]}{#1} \else \ifx \la@currformat \la@wolframformat \la@writecodefile{Print[#2]}{#1} \else \la@unknownoption{wolfram}{#1} \fi\fi \la@executewolfram{} } \input{\la@resulttempfile}%% \la@cleancodetempfile% }

\newcommand{\wolframalpha}[2][tex]{% \la@notformathics{wolframalpha}% \def\la@currformat{#1}% \la@cleancodetempfile% \la@writecodefile{WolframAlpha["#2","% \ifx\la@currformat\la@textformat ShortAnswer\else \ifx\la@currformat\la@wolframtwoformat Result\else WolframResult\fi\fi "]}{#1}% \ifx \la@currformat \la@textformat \la@executewolfram{} \else \ifx \la@currformat \la@wolframformat \la@executewolfram{} \else \ifx \la@currformat \la@wolframtwoformat \la@executewolfram{} \else \ifx \la@currformat \la@texformat \la@executewolfram{TeXForm} \else \la@unknownoption{wolframalpha}{#1} \fi\fi\fi\fi \input{\la@resulttempfile}% \la@cleancodetempfile% }

\newcommand{\wolframgraphics}[3][pdf]{% \def\la@currformat{#1}% \la@cleancodetempfile% \notbool{ifmathics}{% \la@writecodefile{#2}{#1}% \ifx \la@currformat \la@pdfformat \la@executewolfram{PDF} \else \ifx \la@currformat \la@pngformat \la@executewolfram{PNG} \else \ifx \la@currformat \la@jpgformat \la@executewolfram{JPEG} \else \la@unknownoption{wolframgraphics}{#1} \fi\fi\fi \ifx \la@currformat \la@pdfformat \immediate\write18{cp\space\la@resulttempfile\space#3.pdf} \else \ifx \la@currformat \la@pngformat \immediate\write18{cp\space\la@resulttempfile\space#3.png} \else \ifx \la@currformat \la@jpgformat \immediate\write18{cp\space\la@resulttempfile\space#3.jpg} \else \la@unknownoption{wolframgraphics}{#1} \fi\fi\fi }{% \la@writecodefile{Print[TeXForm[#2]]}{asy} \la@executewolfram{} \immediate\write18{cp\space\la@resulttempfile\space#3.asy} } }

\newcommand{\wolframsolve}[2]{% \la@cleancodetempfile% \def\la@solvecode{"@backslashchar@backslashchar begin{flalign}"<>StringJoin@@("@ampchar"<>ToString[ToExpression["TeXForm"][#2]]<>"="<>ToString[ToExpression["TeXForm"][@hashchar]]<>"@backslashchar@backslashchar@backslashchar@backslashchar"@ampchar/@(#2/.Solve[#1, #2]))<>"@backslashchar@backslashchar end{flalign}"} \notbool{ifmathics}{% \la@writecodefile{\la@solvecode}{}% \la@executewolfram{Text}% }{% \la@writecodefile{Print[\la@solvecode]}{}% \la@executewolfram{}% } \input{\la@resulttempfile}% \la@cleancodetempfile% }

\newcommand{\wolframdsolve}[3]{% \la@cleancodetempfile% \def\la@solvecode{"@backslashchar@backslashchar begin{flalign}"<>StringJoin@@("@ampchar"<>ToString[ToExpression["TeXForm"][#2]]<>"="<>ToString[ToExpression["TeXForm"][@hashchar]]<>"@backslashchar@backslashchar@backslashchar@backslashchar"@ampchar/@(#2/.DSolve[#1, #2, #3]))<>"@backslashchar@backslashchar end{flalign}"} \notbool{ifmathics}{% \la@writecodefile{\la@solvecode}{}% \la@executewolfram{Text}% }{% \la@writecodefile{Print[\la@solvecode]}{}% \la@executewolfram{}% } \input{\la@resulttempfile}% \la@cleancodetempfile% }

\newcommand{\wolframtex}[2][tex]{% \la@notformathics{wolframtex}% \def\la@currformat{#1}% \la@cleancodetempfile% \la@writecodefile{ToExpression["\unexpanded{#2}", TeXForm]}{#1}% \immediate\write18{sed -i '' 's/@backslashchar@backslashchar/@backslashchar@backslashchar@backslashchar@backslashchar/g' \la@codetempfile}% \ifx \la@currformat \la@texformat \la@executewolfram{TeXForm} \else \ifx \la@currformat \la@textformat \la@executewolfram{Text} \else \ifx \la@currformat \la@wolframformat \la@executewolfram{} \else \la@unknownoption{wolframtex}{#1} \fi\fi\fi \input{\la@resulttempfile}% \la@cleancodetempfile% }

\newcommand{\wolframanimation}[2]{% \la@notformathics{wolframanimation}% \la@cleancodetempfile% \la@writecodefile{#1}{}% \la@executewolfram{GIF} \immediate\write18{cp\space\la@resulttempfile\space#2.gif}% \immediate\write18{mkdir -p #2}% \immediate\write18{convert -coalesce #2.gif #2/frame.png}% \la@cleancodetempfile% }

\newcommand{\wolframtable}[1]{% \la@notformathics{wolframtable}% \la@cleancodetempfile% \def\la@tablecode{StringJoin@@((StringRiffle[("$"<>Slot[1]<>"$")@ampchar/@ToString/@TeXForm/@(Slot[1]),"\space@ampchar\space"]<>"@backslashchar@backslashchar@backslashchar@backslashchar")@ampchar/@(#1))}% \la@writecodefile{\la@tablecode}{}% \la@executewolfram{Text}% \la@cleancodetempfile% \input{\la@resulttempfile}% }

\AtBeginDocument{% %^^A check if shell-escape is available \ifcase\pdf@shellescape \PackageError{latexalpha2}{Shell escape is not enabled, which is required to use latexalpha2}{}\or \PackageInfo{latexalpha2}{Shell escape is successfully enabled}{}\or \PackageError{latexalpha2}{Shell escape is not enabled, which is required to use latexalpha2}{}\fi

%^^A check if wolframscript or mathics is available \immediate\write18{command -v \notbool{ifmathics}{wolframscript}{mathics}; echo $? > latexalpha2_check.tmp}% \newread\wsreturncodefile% \immediate\openin\wsreturncodefile=latexalpha2_check.tmp% \readline\wsreturncodefile to \wsreturncode% \immediate\closein\wsreturncodefile% %^^A https://tex.stackexchange.com/a/26873 \def\instring#1#2{TT\fi\begingroup\edef\x{\endgroup\noexpand\in@{#1}{#2}}\x\ifin@}% \notbool{ifmathics}{% \if\instring{0}{\wsreturncode} \PackageInfo{latexalpha2}{WolframScript is available}{}\else \PackageError{latexalpha2}{WolframScript cannot be found, which is required to use latexalpha2}{}\fi }{% \if\instring{0}{\wsreturncode} \PackageInfo{latexalpha2}{Mathics is available}{}\else \PackageError{latexalpha2}{Mathics cannot be found, which is required to use the Mathics mode of latexalpha2}{}\fi } }

\AtEndDocument{% \ifbool{ifcache}{}{\immediate\write18{rm -f .latexalpha2_*.out}} } \endinput %% %% End of file `latexalpha2.sty'.

Diaa
  • 9,599
  • 1
    why don't you try yourself? Go through the code, look for all \immediate\write18 lines and then consider if the call makes sense on windows and if not how to translate it. – Ulrike Fischer Jan 11 '21 at 18:55
  • @UlrikeFischer if I know how to do it, I wouldn't ask on this Q/A site in the first place. – Diaa Jan 11 '21 at 19:58
  • 1
    you don't have to know, I don't know it either. You only need to be able to find it out and test. (I don't have wolfram, so I can't test anything at all) – Ulrike Fischer Jan 11 '21 at 23:47
  • 1
    It's probably sufficient to replace wolframscript by C:\full\path\to\wolframscript.exe in \la@wolframscript and remove the installation check in \AtBeginDocument. – Henri Menke Jan 16 '21 at 20:56

2 Answers2

3

Updated answer

An answer for question posted: adapting latexalpha2 for windows and lualatex / pdflatex.

c

xx

This code test most of the functions of the original package.

\documentclass[12pt,a4paper]{article}
\RequirePackage{float}%

\RequirePackage[cache]{latexalpha2wlua} % option cache (faster the second run) or nocache (recalculate always)

\begin{document}

\begin{figure}[H] \wolframgraphics{Plot3D[Sin[x]Cos[y],{x,-2Pi,2Pi},{y,-2Pi,2Pi}]}{Plot3Db} \includegraphics{Plot3Db.pdf} \caption{Plot 3D of $f(x,y)=\sin(x)\cos(y)$} \centering \end{figure}

Laplace Transform of $t^4 sin(t)$

$$ \wolfram{LaplaceTransform[t^4 Sin[t],t,s]} $$

Generates a power series expansion

$$ \wolfram{Series[Exp[x],{x,0,5}]} $$

\bigskip

Compton scattering for electron \bigskip

$\wolframalpha{Compton scattering for electron}$

\bigskip

Solve the equation $a x^2+b x+c=0$ and display the corresponding results

\wolframsolve{a x^2+b x+c==0}{x}

\newpage \begin{table}[H] \centering \begin{tabular}{cccc} %set the correct number of columns! \toprule \wolframtable{Join[{{x,x^2,x^3,x^4}}, Table[{i,i^2,i^3,i^4},{i,6}]]} \end{tabular} \caption{Generate a table $x$, $x^2$, $x^3$, $x^4$} \end{table}

\bigskip

The population of Shanghai is $\wolframalpha{Shanghai population}$, which is $\wolframalpha{ratio of Shanghai population and NYC population}$ times the population of New York City.

\bigskip

Solve differential equations $y'(x) + y(x) = a sin(x)y(x)*x $

\wolframdsolve{y'[x]+y[x]==a Sin[x]}{y[x]}{x}

\end{document}

The package, that I renamed latexalpha2wlua, works with windows and lualatex.

%%
%% This is file `latexalpha2wlua.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% latexalpha2.dtx  (with options: `package')
%% 
%% This is a generated file.
%% 
%% Copyright (C) 2019 by Yi Liu <me@yliu.io>
%% 
%% This file may be distributed and/or modified under the conditions of
%% the LaTeX Project Public License, either version 1.3c of this license
%% or (at your option) any later version. The latest version of this
%% license is in:
%% 
%%    http://www.latex-project.org/lppl.txt
%% 
%% and version 1.3c or later is part of all distributions of LaTeX
%% version 2006/05/20 or later.
%%
%% modified for windows and luatex by Simon Dispa (2021) v1.0e
%% 
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{latexalpha2wlua}[2021/01/21 v1.0d latexalpha2wlua]
\RequirePackage{graphicx}
\RequirePackage{amsmath}
\RequirePackage{etoolbox}
\RequirePackage{pdftexcmds}
\RequirePackage{shellesc}
\RequirePackage{booktabs}%
\RequirePackage{animate}

\newbool{ifcache}

\DeclareOption{local}{\def\la@platform{-local}} \DeclareOption{cloud}{\def\la@platform{-cloud}} \DeclareOption{cache}{\booltrue{ifcache}} \DeclareOption{nocache}{\boolfalse{ifcache}}

\ExecuteOptions{local} \ProcessOptions\relax

\begingroup \makeatletter \catcode#=12 \catcode&=12 \gdef@hashchar{#} \gdef@ampchar{&} \gdef\backslash{@backslashchar} \makeatother \endgroup

\def\la@codetempfile{la2w_code.wls} \def\la@resulttempfile{} \def\la@currcodehash{} \def\la@animaoutdir{} \def\la@outdir{la_output} \def\la@midrule{midrule}

\newcommand{\la@unknownoption}[2]{\PackageError{latexalpha2wlua}{Unknown option '#2' for @backslashchar #1}{}}

\newcommand{\la@makedir}[1]{IF NOT EXIST #1 md #1} \newcommand{\la@deldir}[1]{rd /Q /S #1 }

\newcommand{\la@executewolfram}[1]{% \IfFileExists{\la@resulttempfile}{\ifbool{ifcache}{}{\la@executewolframcore{#1}} }{\la@executewolframcore{#1}} }

\newcommand{\la@executewolframcore}[1]{% \def\la@wolframscript{wolframscript -file \la@outdir\backslash\la@codetempfile\space -print -format #1 \la@platform\space> \la@resulttempfile}% \immediate\write18{\la@wolframscript}% }

\newcommand{\la@writecodefile}[2]{% \gdef\la@currcodehash{\pdf@mdfivesum{#1#2}}%%<<<<<<<<<<<<<<<<<<<<<<<<<<<< \gdef\la@resulttempfile{\la@outdir\backslash#2_\la@currcodehash.out}% \newwrite\tempfile% \immediate\openout\tempfile=\la@outdir\backslash\la@codetempfile% \immediate\write\tempfile{#1}% \immediate\closeout\tempfile% }

\def\la@texformat{tex} \def\la@textformat{txt} \def\la@wolframformat{wolfram} \def\la@wolframtwoformat{wolfram2} \def\la@pdfformat{pdf} \def\la@pngformat{png} \def\la@jpgformat{jpg}

\newcommand{\wolfram}[2][tex]{% \message{>>>> starting wolfram (#1): please wait ... ^^J}% \def\la@currformat{#1}% \la@writecodefile{#2}{#1}% \ifx \la@currformat \la@texformat \la@executewolfram{TeXForm} \else \ifx \la@currformat \la@textformat \la@executewolfram{txt} \else \ifx \la@currformat \la@wolframformat \la@executewolfram{} \else \la@unknownoption{wolfram}{#1} \fi\fi\fi \input{\la@resulttempfile}% }

\newcommand{\wolframalpha}[2][tex]{% \message{>>>> starting wolframalpha (-> #1): please wait ... ^^J}% \def\la@currformat{#1}% \la@writecodefile{WolframAlpha["#2","% \ifx\la@currformat\la@textformat ShortAnswer\else \ifx\la@currformat\la@wolframtwoformat Result\else WolframResult\fi\fi "]}{#1}% \ifx \la@currformat \la@textformat \la@executewolfram{} \else \ifx \la@currformat \la@wolframformat \la@executewolfram{} \else \ifx \la@currformat \la@wolframtwoformat \la@executewolfram{} \else \ifx \la@currformat \la@texformat \la@executewolfram{TeXForm} \else \la@unknownoption{wolframalpha}{#1} \fi\fi\fi\fi \input{\la@resulttempfile}% }

\newcommand{\wolframgraphics}[3][pdf]{% \message{>>>> starting wolfram #1 graphics: please wait ... ^^J}% \def\la@currformat{#1}% \la@writecodefile{#2}{#1}% \ifx \la@currformat \la@pdfformat \la@executewolfram{PDF} \else \ifx \la@currformat \la@pngformat \la@executewolfram{PNG} \else \ifx \la@currformat \la@jpgformat \la@executewolfram{JPEG} \else \la@unknownoption{wolframgraphics}{#1} \fi\fi\fi \ifx \la@currformat \la@pdfformat \immediate\write18{copy\space\la@resulttempfile\space#3.pdf >NUL} \else \ifx \la@currformat \la@pngformat \immediate\write18{copy\space\la@resulttempfile\space#3.png >NUL} \else \ifx \la@currformat \la@jpgformat \immediate\write18{copy\space\la@resulttempfile\space#3.jpg >NUL} \else \la@unknownoption{wolframgraphics}{#1} \fi\fi\fi }

\newcommand{\wolframsolve}[2]{% \def\la@currformat{#2}% \message{>>>> starting wolfram (-> tex) solve: please wait ... ^^J}% \la@writecodefile{"@backslashchar@backslashchar begin{flalign}"<>StringJoin@@("@ampchar"<>ToString[ToExpression["TeXForm"][#2]]<>"="<>ToString[ToExpression["TeXForm"][@hashchar]]<>"@backslashchar@backslashchar@backslashchar@backslashchar"@ampchar/@(#2/.Solve[#1, #2]))<>"@backslashchar@backslashchar end{flalign}"}{}% \la@executewolfram{txt}% \input{\la@resulttempfile}% }

\newcommand{\wolframdsolve}[3]{% \message{>>>> starting wolfram (-> tex) diff. eq. solve: please wait ... ^^J}% \la@writecodefile{"@backslashchar@backslashchar begin{flalign}"<>StringJoin@@("@ampchar"<>ToString[ToExpression["TeXForm"][#2]]<>"="<>ToString[ToExpression["TeXForm"][@hashchar]]<>"@backslashchar@backslashchar@backslashchar@backslashchar"@ampchar/@(#2/.DSolve[#1, #2, #3]))<>"@backslashchar@backslashchar end{flalign}"}{}% \la@executewolfram{txt}% \input{\la@resulttempfile}% }

\newcommand{\wolframtable}[1]{% \message{>>>> starting wolfram table: please wait ... ^^J}% \def\la@tablecode{StringJoin@@(% (StringRiffle[("$"<>Slot[1]<>"$") @ampchar/@ToString/@TeXForm/@(Slot[1]),"\space@ampchar\space" ] <> "\space@backslashchar@backslashchar@backslashchar@backslashchar\space@backslashchar@backslashchar\la@midrule\space") @ampchar/@(#1)
)}% \la@writecodefile{\la@tablecode}{tex}% \la@executewolfram{Text}% \input{\la@resulttempfile}% } \newcommand{\wolframonlygif}[2]{% \message{>>>> starting wolfram only gif: please wait wait wait ... ^^J}% \la@writecodefile{#1}{gif}% \la@executewolfram{GIF} \immediate\write18{copy\space\la@resulttempfile\space #2.gif >NUL}% }

\newcommand{\wolframanimation}[2]{% \message{>>>> starting wolfram animation: please wait wait wait ... ^^J}% \gdef\la@animaoutdir{#2}
\immediate\write18{\la@makedir{\la@animaoutdir}} \la@writecodefile{#1}{gif}% \la@executewolfram{GIF} \immediate\write18{copy\space\la@resulttempfile\space\la@animaoutdir\backslash#2.gif >NUL}% OK \immediate\write18{convert -coalesce \space\la@animaoutdir\backslash#2.gif #2/frame.png}% }

\AtBeginDocument{%
\immediate\write18{\la@makedir{\la@outdir}} %% Check if wolframscript is available \immediate\write18{wolframscript -code 2+2 > la2_check.tmp}% \newread\wsreturncodefile% \immediate\openin\wsreturncodefile= la2_check.tmp% \readline\wsreturncodefile to \wsreturncode% \immediate\closein\wsreturncodefile% % %^^A https://tex.stackexchange.com/a/26873 \def\instring#1#2{TT\fi\begingroup\edef\x{\endgroup\noexpand\in@{#1}{#2}}\x\ifin@}% \if\instring{4}{\wsreturncode} \immediate\write18{del /Q la2_check.tmp >NUL} \PackageInfo{latexalpha2wlua}{WolframScript is available}{}\else \PackageError{latexalpha2wlua}{WolframScript cannot be found. Required to use latexalpha2wlua}{}
\fi }

\AtEndDocument{% \ifbool{ifcache}{}{\immediate\write18{\la@deldir{\la@outdir}}} }

\endinput %% %% End of file `latexalpha2wlua.sty'.

Notes

(0) Wolfram Engine must be installed and wolframscript must be fully working on the command prompt! Examples might be found in

https://reference.wolfram.com/language/ref/program/wolframscript.html

(1) Tested with windows 7.

(2) The file latexalpha2wlua.sty should be in the same directory of the .tex document.

(3) Tested with lualatex and pdflatex. See the MWE

(4) The code was simplified, eliminating the harmless messages.

(5) Using the option cache makes the recompile much faster if the query does not change. For the time being all the output files remain in the same directory. The option nocache clears everything, but recalculates all the second time.

(6) To make animated GIFs with the same package see

https://tex.stackexchange.com/a/580062/161015

Simon Dispa
  • 39,141
  • many thanks for your answer. I use lualatex all the time not pdflatex. Do you mean that your code won't work with lualatex or just works with limitations? – Diaa Jan 17 '21 at 21:58
  • @Diaa It woks with lualatex up to my MWE. Up to you to test the full functionality . I updated the answer. – Simon Dispa Jan 17 '21 at 22:16
  • It normally works with me but I don't understand why I receive many messages in texstudio like these 'command' is not recognized as an internal or external command, operable program or batch file. Could Not Find C:\Users\Diaa\Desktop\Test\latexalpha2_code.tmp as shown here https://i.ibb.co/Fn8YcXq/image.png – Diaa Jan 17 '21 at 22:45
  • They are a nuisance. Windows says it can't find the .tmp files to delete. "Apparently" it does not affect the operation of the class. I'm tracing the origin because I also like tidier things. – Simon Dispa Jan 17 '21 at 23:05
  • Each command is deleting the temp code file (via \la@cleancodetempfile) twice at the start and end of the command definition. If we only keep the cleaning process at the end of each command, no error would show up. Now, I am trying to resolve the error of command which comes from this line \immediate\write18{command wolframscript; echo $? > latexalpha2_check.tmp} I think. Also, I don't know why this \ifbool{ifcache}{}{\immediate\write18{del/Q *.out} } doesn't delete *out files. – Diaa Jan 17 '21 at 23:18
  • OK! The deletion \immediate\write18{del/Q *.out} should be in the true condition of ifcache not the false one \ifbool{ifcache}{\immediate\write18{del/Q *.out} }{}. – Diaa Jan 17 '21 at 23:33
  • I fixed all the issues and will post a complementary answer tomorrow.. Thanks for your help :) – Diaa Jan 18 '21 at 01:56
  • I have some points on your great answer: (1) you accidentally delete the definition \newcommand{\la@unknownoption}[2]{\PackageError{latexalpha2}{Unknown option '#2' for #1}{}} from your file. (2) For the copy command I think copy /y is better. (3) Have you intentionally removed the command \wolframanimation? (4) You forgot to move the deletion to the true condition of ifcache here \ifbool{ifcache}{}{\immediate\write18{del /Q *.out >NUL} }. – Diaa Jan 19 '21 at 13:05
  • Also, I think the delete command should better be del /f /q <the file> 2>nul. For the sake of completeness, it would be nice to have examples of \wolframtable, \wolframdsolve, and \wolframanimation. – Diaa Jan 19 '21 at 13:22
  • Furthermore, why does the option cloud fail with \wolframalpha{Shanghai population} and \wolframalpha{ratio of Shanghai population and NYC population}? – Diaa Jan 19 '21 at 13:30
  • \wolframtable and \wolframdsolve examples are included in the MWE, I did not test \wolframanimation yet. – Simon Dispa Jan 19 '21 at 13:43
  • \RequirePackage[cache]{latexalpha2wlua} keeps output files for reuse. \RequirePackage[nocache]{latexalpha2wlua} deletes them on exit – Simon Dispa Jan 19 '21 at 13:50
  • May I know why you used extension wsl for the temporary code file? – Diaa Jan 22 '21 at 23:44
  • Its is useful for debugging. I some cases it can fed into wolfrascript from the console. – Simon Dispa Jan 23 '21 at 13:25
0

This is my own take on Simon Dispa's great original answer.

%% This is file `latexalpha2Win.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% latexalpha2.dtx  (with options: `package')
%% 
%% This is a generated file.
%% 
%% Copyright (C) 2019 by Yi Liu <me@yliu.io>
%% 
%% This file may be distributed and/or modified under the conditions of
%% the LaTeX Project Public License, either version 1.3c of this license
%% or (at your option) any later version. The latest version of this
%% license is in:
%% 
%%    http://www.latex-project.org/lppl.txt
%% 
%% and version 1.3c or later is part of all distributions of LaTeX
%% version 2006/05/20 or later.
%% modified for windows and luatex by Simon Dispa (2021) 
%% https://tex.stackexchange.com/a/579254/2288
%% and Diaa 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{latexalpha2Win}
  [2021/01/19 v1.0 latexalpha2Win]
\RequirePackage{graphicx}
\RequirePackage{amsmath}
\RequirePackage{etoolbox}
\RequirePackage{pdftexcmds}
\RequirePackage{shellesc}

\newbool{ifcache}

\DeclareOption{local}{\def\la@platform{-local}} \DeclareOption{cloud}{\def\la@platform{-cloud}} \DeclareOption{cache}{\booltrue{ifcache}} \DeclareOption{nocache}{\boolfalse{ifcache}}

\ExecuteOptions{local,cache} \ProcessOptions\relax

\begingroup \makeatletter \catcode#=12 \catcode&=12 \gdef@hashchar{#} \gdef@ampchar{&} \gdef\backslash{@backslashchar} \makeatother \endgroup

\def\la@codetempfile{latexalpha2_code.tmp} \def\la@resulttempfile{} \def\la@currcodehash{}

\newcommand{\la@cleancodetempfile}{\immediate\write18{del /F \la@codetempfile}} \newcommand{\la@unknownoption}[2]{\PackageError{latexalpha2}{Unknown option '#2' for #1}{}}

\newcommand{\la@executewolfram}[1]{% \IfFileExists{\la@resulttempfile}{ \ifbool{ifcache}{\message{found cached output file \la@resulttempfile^^J}}{\la@executewolframcore{#1}} }{\la@executewolframcore{#1}} }

\newcommand{\la@executewolframcore}[1]{% \def\la@wolframscript{wolframscript -file \la@codetempfile\space -print \ifblank{#1}{}{-format #1} \la@platform\space> \la@resulttempfile}% \message{command: \la@wolframscript^^J}% \immediate\write18{\la@wolframscript}% }

\newcommand{\la@writecodefile}[2]{% \message{------------ latexalpha2 x-------------^^J}% \message{wolfram code: #1^^J}% \ifblank{#2}{}{\message{option: #2}}% \gdef\la@currcodehash{\pdf@mdfivesum{#1#2}}% \gdef\la@resulttempfile{.latexalpha2_\la@currcodehash.out}% \newwrite\tempfile% \immediate\openout\tempfile=\la@codetempfile% \immediate\write\tempfile{#1}% \immediate\closeout\tempfile% }

\def\la@texformat{tex} \def\la@textformat{text} \def\la@wolframformat{wolfram} \def\la@wolframtwoformat{wolfram2} \def\la@pdfformat{pdf} \def\la@pngformat{png} \def\la@jpgformat{jpg}

\newcommand{\wolfram}[2][tex]{% \def\la@currformat{#1}% \la@writecodefile{#2}{#1}% \ifx \la@currformat \la@texformat \la@executewolfram{TeXForm} \else \ifx \la@currformat \la@textformat \la@executewolfram{Text} \else \ifx \la@currformat \la@wolframformat \la@executewolfram{} \else \la@unknownoption{wolfram}{#1} \fi\fi\fi \input{\la@resulttempfile}% \la@cleancodetempfile% }

\newcommand{\wolframalpha}[2][tex]{% \def\la@currformat{#1}% \la@writecodefile{WolframAlpha["#2","% \ifx\la@currformat\la@textformat ShortAnswer\else \ifx\la@currformat\la@wolframtwoformat Result\else WolframResult\fi\fi "]}{#1}% \ifx \la@currformat \la@textformat \la@executewolfram{} \else \ifx \la@currformat \la@wolframformat \la@executewolfram{} \else \ifx \la@currformat \la@wolframtwoformat \la@executewolfram{} \else \ifx \la@currformat \la@texformat \la@executewolfram{TeXForm} \else \la@unknownoption{wolframalpha}{#1} \fi\fi\fi\fi \input{\la@resulttempfile}% \la@cleancodetempfile% }

\newcommand{\wolframgraphics}[3][pdf]{% \def\la@currformat{#1}% \la@writecodefile{#2}{#1}% \ifx \la@currformat \la@pdfformat \la@executewolfram{PDF} \else \ifx \la@currformat \la@pngformat \la@executewolfram{PNG} \else \ifx \la@currformat \la@jpgformat \la@executewolfram{JPEG} \else \la@unknownoption{wolframgraphics}{#1} \fi\fi\fi \ifx \la@currformat \la@pdfformat \immediate\write18{copy \la@resulttempfile #3.pdf} \else \ifx \la@currformat \la@pngformat \immediate\write18{copy \la@resulttempfile #3.png} \else \ifx \la@currformat \la@jpgformat \immediate\write18{copy \resulttempfile #3.jpg} \else \la@unknownoption{wolframgraphics}{#1} \fi\fi\fi \la@cleancodetempfile }

\newcommand{\wolframsolve}[2]{% \la@writecodefile{"@backslashchar@backslashchar begin{flalign}"<>StringJoin@@("@ampchar"<>ToString[ToExpression["TeXForm"][#2]]<>"="<>ToString[ToExpression["TeXForm"][@hashchar]]<>"@backslashchar@backslashchar@backslashchar@backslashchar"@ampchar/@(#2/.Solve[#1, #2]))<>"@backslashchar@backslashchar end{flalign}"}{}% \la@executewolfram{Text}% \input{\la@resulttempfile}% \la@cleancodetempfile% }

\newcommand{\wolframdsolve}[3]{% \la@writecodefile{"@backslashchar@backslashchar begin{flalign}"<>StringJoin@@("@ampchar"<>ToString[ToExpression["TeXForm"][#2]]<>"="<>ToString[ToExpression["TeXForm"][@hashchar]]<>"@backslashchar@backslashchar@backslashchar@backslashchar"@ampchar/@(#2/.DSolve[#1, #2, #3]))<>"@backslashchar@backslashchar end{flalign}"}{}% \la@executewolfram{Text}% \input{\la@resulttempfile}% \la@cleancodetempfile% }

\newcommand{\wolframtex}[2][tex]{% \def\la@currformat{#1}% \la@writecodefile{ToExpression["\unexpanded{#2}", TeXForm]}{#1}% \immediate\write18{sed -i '' 's/@backslashchar@backslashchar/@backslashchar@backslashchar@backslashchar@backslashchar/g' \la@codetempfile}% \ifx \la@currformat \la@texformat \la@executewolfram{TeXForm} \else \ifx \la@currformat \la@textformat \la@executewolfram{Text} \else \ifx \la@currformat \la@wolframformat \la@executewolfram{} \else \la@unknownoption{wolframtex}{#1} \fi\fi\fi \input{\la@resulttempfile}% \la@cleancodetempfile% }

\newcommand{\wolframanimation}[2]{% \la@writecodefile{#1}{}% \la@executewolfram{GIF} \immediate\write18{copy \space\la@resulttempfile\space#2.gif}% \immediate\write18{md #2}% \immediate\write18{convert -coalesce #2.gif #2/frame.png}% \la@cleancodetempfile% }

\AtBeginDocument{% % Check if shell-escape is available \ifcase\pdf@shellescape \PackageError{latexalpha2}{Shell escape is not enabled, which is required to use latexalpha2}{}\or \PackageInfo{latexalpha2}{Shell escape is successfully enabled}{}\or \PackageError{latexalpha2}{Shell escape is not enabled, which is required to use latexalpha2}{}\fi

% Check if wolframscript is available \immediate\write18{where wolframscript >latexalpha2_check.tmp 2>nul}% % https://stackoverflow.com/a/35408403/2849383 %\immediate\write18{where /q wolframscript \ampchar echo @percentchar ERRORLEVEL@percentchar > latexalpha2_check.tmp}% % https://superuser.com/a/1618520/452161 % https://superuser.com/a/718194/452161 % https://stackoverflow.com/a/25696405/2849383 \newread\wsreturncodefile% \immediate\openin\wsreturncodefile=latexalpha2_check.tmp% \readline\wsreturncodefile to \wsreturncode% \immediate\closein\wsreturncodefile% % https://tex.stackexchange.com/a/26873 \def\instring#1#2{TT\fi\begingroup\edef\x{\endgroup\noexpand\in@{#1}{#2}}\x\ifin@}% \if\instring{{wolframscript.exe}}{\wsreturncode} \PackageInfo{latexalpha2}{WolframScript is available}{} \immediate\write18{del /F latexalpha2_check.tmp}\else \immediate\write18{del /F latexalpha2_check.tmp} \PackageError{latexalpha2}{WolframScript cannot be found, which is required to use latexalpha2}{} \fi }

\AtEndDocument{% \ifbool{ifcache}{\immediate\write18{del/Q *.out}}{ } } \endinput %% %% End of file `latexalpha2Win.sty'.

Diaa
  • 9,599