summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex
diff options
context:
space:
mode:
authorKarl Berry <karl@freefriends.org>2023-06-23 20:33:51 +0000
committerKarl Berry <karl@freefriends.org>2023-06-23 20:33:51 +0000
commitdf318de1935235e68ef8894a50a09b8e3bd8e6d4 (patch)
treecc3865453735c26a4d70738d885b886d18579fb2 /Master/texmf-dist/tex
parent75e729bd7c61127bcf2eb1b54739af2a724cfdbb (diff)
runcode (23jun23)
git-svn-id: svn://tug.org/texlive/trunk@67453 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/tex')
-rw-r--r--Master/texmf-dist/tex/latex/runcode/runcode.sty89
1 files changed, 79 insertions, 10 deletions
diff --git a/Master/texmf-dist/tex/latex/runcode/runcode.sty b/Master/texmf-dist/tex/latex/runcode/runcode.sty
index 21e85f99082..1fe5b079568 100644
--- a/Master/texmf-dist/tex/latex/runcode/runcode.sty
+++ b/Master/texmf-dist/tex/latex/runcode/runcode.sty
@@ -4,7 +4,7 @@
%
% This package is based on an ongoing work by Haim Bar and HaiYing Wang, and comments and questions are welcome!
-\ProvidesPackage{runcode}[2023/06/13 runcode v1.9]
+\ProvidesPackage{runcode}[2023/06/23 runcode v2.0]
\newif\ifruncode
% Change to \runcodefalse if you want to suspend code execution
@@ -12,6 +12,15 @@
\DeclareOption{run}{\runcodetrue}
\DeclareOption{cache}{\runcodefalse}
+% to control whether \inln will get the result from the program(R, Python, etc.)
+% or get it from cache (if available), then we define the following. The default
+% is true (to run any \inln command in the tex file)
+\newif\ifinlnrun
+% Change to \inlnrunfalse if you want to get the results from cache for all
+% \inln instances in the tex file (if cache is not available, it will be executed
+% even if this variable is set to false)
+\inlnruntrue
+
% Choose the package for code typesetting. The default is minted.
% The other two options are listings and fvextra. For backward compatibility,
% it is possible to use nominted, in which case fvextra is used.
@@ -144,7 +153,7 @@
\usepackage{xcolor} % for highlighting
\usepackage{inputenc}
\usepackage{textgreek}
-\usepackage{filecontents}
+% \usepackage{filecontents}
\usepackage{xifthen}
\usepackage{xparse}
\usepackage{xstring}
@@ -249,7 +258,7 @@
% codeOutput is used).
% Arg #4 controls when to run the code (optional - if not given, it listens
% to \runcode; run = force the code to run; cache or anything else =
-% use cache)
+% use cache. If the output file does not exist, override the cache option and run the code)
\NewDocumentCommand{\runExtCode}{m m m O{}}{
\IfFileExists{#2}{
\stepcounter{codeOutput}
@@ -264,11 +273,13 @@
}{
\ifstrequal{#4}{run}{\immediate\write18{#1 #2 > \tmpname }}{}
}
+ \IfFileExists{\tmpname}{}{\immediate\write18{#1 #2 > \tmpname }}
}{
\textcolor{red}{\textbf{runExtCode: File #2 does not exist!}}
}
}
+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \includeOutput[Arg #1]{Arg #2} is used to embed the output from executed code.
% Arg #1 is the output file name (optional - if not given, the counter
@@ -314,16 +325,19 @@
% and embed resulting output.
% Arg #1 is the executable program;
% Arg #2 is the source code;
-% Arg #3 is the output file name (optional);
+% Arg #3 is the output file name (optional).
% Arg #4 is the type output (if skipped or with empty value the default type is
-% inline; vbox = verbatim in a box);
+% inline; vbox = verbatim in a box); If you append .cache to the argument
+%. AND the output file in Arg#3 exists, then \inln will use the cached result;
\NewDocumentCommand{\inln}{m m O{} O{inline}}{\-
\unskip
\ifthenelse{\isempty{#3}}
{\stepcounter{codeOutput}\unskip\unskip\setvalue{\tmpname}{\generated/\jobname_inln\thecodeOutput}\unskip\unskip\unskip}
{\unskip\setvalue{\tmpname}{\generated/#3}\unskip\unskip\unskip}
- \unskip\unskip\unskip
+ \unskip\unskip\unskip
+ \IfFileExists{\tmpname.tex}{\IfEndWith{#4}{cache}{\inlnrunfalse}{\inlnruntrue}}{\inlnruntrue}
+ \ifinlnrun
\ifruncode % cache mode - don't try to run the code, just get the previous results
\IfBeginWith{#2}{```}{\ifruncode\immediate\write18{#1 > \tmpname.tex}\unskip\fi}
{\newwrite\tempfile
@@ -333,10 +347,12 @@
\ifruncode
\immediate\write18{#1 \tmpname.txt > \tmpname.tex}\unskip
\fi}
- \fi % end cache mode
+ \fi
+ \fi% end cache mode
+ \unskip\unskip\unskip
\IfFileExists{\tmpname.tex}
{\checkZeroBytes{\tmpname.tex}\unskip
- \ifstrequal{#4}{vbox}
+ \IfBeginWith{#4}{vbox}
{\begin{tcolorbox}
\ifminted\unskip
\inputminted{text}{\tmpname.tex}\unskip
@@ -350,6 +366,29 @@
{\unskip{\input{\tmpname.tex}}\unskip}}
{\textcolor{red}{\textbf{Output file \tmpname~ not found}}}}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% Extended and language-specific comments:
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% \runCodeIncOut: runs an external code and embeds the output.
+% Arg #1 is the executable program,
+% Arg #2 is the source file name,
+% Arg #3 controls when to run the code (optional - if not given, it listens
+% to \runcode; run = force the code to run; cache or anything else =
+% use cache). The same functionality as that of Arg #4 of \runExtCode.
+% Arg #4 is the output file name (optional - if not given, the counter
+% codeOutput is used). The same functionality as that of Arg #4 of
+% \runExtCode.
+% Arg #5 is the optional type output with default "vbox"
+% (vbox = verbatim in a box, tex = pure latex, or inline)
+% The same functionality as that of Arg #2 of \includeOutput.
+
+\NewDocumentCommand{\runCodeIncOut}{m m O{} O{} O{vbox}}{
+ \runExtCode{#1}{#2}{#4}[#3]
+ \includeOutput{#4}[#5]
+}
+
+% R
\NewDocumentCommand{\runR}{O{python3 -c 'from talk2stat.talk2stat import client; client("./","R","#2")'} m m O{}}
{
\def\runcmd{python3 -c 'from talk2stat.talk2stat import client; client("./","R","#2")'}
@@ -358,8 +397,16 @@
{\runExtCode{#1}{#2}{#3}[#4]}
}
+\NewDocumentCommand{\runRIncOut}
+{O{python3 -c 'from talk2stat.talk2stat import client; client("./","R","#2")'} m O{} O{} O{vbox}}
+{\def\runcmd{python3 -c 'from talk2stat.talk2stat import client; client("./","R","#2")'}
+ \ifstrequal{#1}{\runcmd}
+ {\runCodeIncOut{#1}{}[#3][#4][#5]}
+ {\runCodeIncOut{#1}{#2}[#3][#4][#5]}}
+
\NewDocumentCommand{\inlnR}{O{python3 -c 'from talk2stat.talk2stat import client; client("./","R","\tmpname.txt")'} m O{} O{inline}}{\IfBeginWith{#2}{```}{\inln{python3 -c 'from talk2stat.talk2stat import client;rc0="""#2""";rc0=rc0.lstrip("```").rstrip("```");client("./","R",f"```{rc0}```")'}{#2}[#3][#4]}{\inln{#1}{#2}[#3][#4]}}
+% Julia
\NewDocumentCommand{\runJulia}{O{python3 -c 'from talk2stat.talk2stat import client; client("./","julia","#2")'} m m O{}}
{
\def\runcmd{python3 -c 'from talk2stat.talk2stat import client; client("./","julia","#2")'}
@@ -368,8 +415,16 @@
{\runExtCode{#1}{#2}{#3}[#4]}
}
+\NewDocumentCommand{\runJuliaIncOut}
+{O{python3 -c 'from talk2stat.talk2stat import client; client("./","julia","#2")'} m O{} O{} O{vbox}}
+{\def\runcmd{python3 -c 'from talk2stat.talk2stat import client; client("./","julia","#2")'}
+ \ifstrequal{#1}{\runcmd}
+ {\runCodeIncOut{#1}{}[#3][#4][#5]}
+ {\runCodeIncOut{#1}{#2}[#3][#4][#5]}}
+
\NewDocumentCommand{\inlnJulia}{O{python3 -c 'from talk2stat.talk2stat import client; client("./","julia","\tmpname.txt")'} m O{} O{inline}}{\IfBeginWith{#2}{```}{\inln{python3 -c 'from talk2stat.talk2stat import client;rc0="""#2""";rc0=rc0.lstrip("```").rstrip("```");client("./","julia",f"```{rc0}```")'}{#2}[#3][#4]}{\inln{#1}{#2}[#3][#4]}}
+% Matlab
\NewDocumentCommand{\runMatlab}{O{python3 -c 'from talk2stat.talk2stat import client; client("./","matlab","#2")'} m m O{}}
{
\def\runcmd{python3 -c 'from talk2stat.talk2stat import client; client("./","matlab","#2")'}
@@ -378,9 +433,16 @@
{\runExtCode{#1}{#2}{#3}[#4]}
}
-\NewDocumentCommand{\inlnMatlab}{O{python3 -c 'from talk2stat.talk2stat import client; client("./","matlab","\tmpname.txt")'} m O{} O{inline}}{\IfBeginWith{#2}{```}{\inln{python3 -c 'from talk2stat.talk2stat import client;rc0="""#2""";rc0=rc0.lstrip("```").rstrip("```");client("./","matlab",f"```{rc0}```")'}{#2}[#3][#4]}{\inln{#1}{#2}[#3][#4]}}
+\NewDocumentCommand{\runMatlabIncOut}
+{O{python3 -c 'from talk2stat.talk2stat import client; client("./","matlab","#2")'} m O{} O{} O{vbox}}
+{\def\runcmd{python3 -c 'from talk2stat.talk2stat import client; client("./","matlab","#2")'}
+ \ifstrequal{#1}{\runcmd}
+ {\runCodeIncOut{#1}{}[#3][#4][#5]}
+ {\runCodeIncOut{#1}{#2}[#3][#4][#5]}}
+\NewDocumentCommand{\inlnMatlab}{O{python3 -c 'from talk2stat.talk2stat import client; client("./","matlab","\tmpname.txt")'} m O{} O{inline}}{\IfBeginWith{#2}{```}{\inln{python3 -c 'from talk2stat.talk2stat import client;rc0="""#2""";rc0=rc0.lstrip("```").rstrip("```");client("./","matlab",f"```{rc0}```")'}{#2}[#3][#4]}{\inln{#1}{#2}[#3][#4]}}
+% Python
\NewDocumentCommand{\runPython}{O{python3 -c 'from talk2stat.talk2stat import client; client("./","python","#2")'} m m O{}}
{
\def\runcmd{python3 -c 'from talk2stat.talk2stat import client; client("./","python","#2")'}
@@ -389,6 +451,13 @@
{\runExtCode{#1}{#2}{#3}[#4]}
}
+\NewDocumentCommand{\runPythonIncOut}
+{O{python3 -c 'from talk2stat.talk2stat import client; client("./","python","#2")'} m O{} O{} O{vbox}}
+{\def\runcmd{python3 -c 'from talk2stat.talk2stat import client; client("./","python","#2")'}
+ \ifstrequal{#1}{\runcmd}
+ {\runCodeIncOut{#1}{}[#3][#4][#5]}
+ {\runCodeIncOut{#1}{#2}[#3][#4][#5]}}
+
\NewDocumentCommand{\inlnPython}{O{python3 -c 'from talk2stat.talk2stat import client; client("./","python","\tmpname.txt")'} m O{} O{inline}}{\IfBeginWith{#2}{```}{\inln{python3 -c 'from talk2stat.talk2stat import client;rc0="""#2""";rc0=rc0.lstrip("```").rstrip("```");client("./","python",f"```{rc0}```")'}{#2}[#3][#4]}{\inln{#1}{#2}[#3][#4]}}
@@ -434,4 +503,4 @@
}
-\endinput
+\endinput \ No newline at end of file