summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/runcode/runcode.sty
diff options
context:
space:
mode:
Diffstat (limited to 'macros/latex/contrib/runcode/runcode.sty')
-rw-r--r--macros/latex/contrib/runcode/runcode.sty55
1 files changed, 42 insertions, 13 deletions
diff --git a/macros/latex/contrib/runcode/runcode.sty b/macros/latex/contrib/runcode/runcode.sty
index 0059a17b86..6737fd213f 100644
--- a/macros/latex/contrib/runcode/runcode.sty
+++ b/macros/latex/contrib/runcode/runcode.sty
@@ -14,6 +14,11 @@
\mintedtrue
\DeclareOption{nominted}{\mintedfalse}
+% Some editors terminate all child processes after LaTeX compiling such as Emacs with Auctex. For this case, use the nohup option. It set the variable notnohup to be false, and the server will not be terminated by the parent process.
+\newif\ifnotnohup
+\notnohuptrue
+\DeclareOption{nohup}{\notnohupfalse}
+
\DeclareOption{R}{
% create a configuration file for R server if it does not exist.
\IfFileExists{R.config}{}{
@@ -27,9 +32,11 @@
}
% Start the server. Need to run just once. Can comment out after the first
% compilation, but remember to terminate the server
- \immediate\write18{python3 -c 'from talk2stat.talk2stat import server,client; server("./","R") if not client("./","R","``` ```") else print("server is already running")'}
- % Some editors terminate all child processes after LaTeX compiling such as Emacs with Auctex. For this case, use the following line instead of the above line.
- % \immediate\write18{nohup python3 -c 'from talk2stat.talk2stat import server,client; server("./","R") if not client("./","R","``` ```") else print("server is already running")' &}
+ \ifnotnohup
+ {\immediate\write18{python3 -c 'from talk2stat.talk2stat import server,client; server("./","R") if not client("./","R","``` ```") else print("server is already running")'}}
+ \else
+ {\immediate\write18{nohup python3 -c 'from talk2stat.talk2stat import server,client; server("./","R") if not client("./","R","``` ```") else print("server is already running")' &}}
+ \fi
}
\DeclareOption{julia}{
@@ -45,8 +52,11 @@
}
% Start the server. Need to run just once. Can comment out after the first
% compilation, but remember to terminate the server
- \immediate\write18{python3 -c 'from talk2stat.talk2stat import server,client; server("./","julia") if not client("./","julia","``` ```") else print("server is already running")'}
- % \immediate\write18{nohup python3 -c 'from talk2stat.talk2stat import server,client; server("./","julia") if not client("./","julia","``` ```") else print("server is already running")' &}
+ \ifnotnohup
+ {\immediate\write18{python3 -c 'from talk2stat.talk2stat import server,client; server("./","julia") if not client("./","julia","``` ```") else print("server is already running")'}}
+ \else
+ {\immediate\write18{nohup python3 -c 'from talk2stat.talk2stat import server,client; server("./","julia") if not client("./","julia","``` ```") else print("server is already running")' &}}
+ \fi
}
\DeclareOption{matlab}{
@@ -62,8 +72,11 @@
}
% Start the server. Need to run just once. Can comment out after the first
% compilation, but remember to terminate the server
- \immediate\write18{python3 -c 'from talk2stat.talk2stat import server,client; server("./","matlab") if not client("./","matlab","``` ```") else print("server is already running")'}
- % \immediate\write18{nohup python3 -c 'from talk2stat.talk2stat import server,client; server("./","matlab") if not client("./","matlab","``` ```") else print("server is already running")' &}
+ \ifnotnohup
+ {\immediate\write18{python3 -c 'from talk2stat.talk2stat import server,client; server("./","matlab") if not client("./","matlab","``` ```") else print("server is already running")'}}
+ \else
+ {\immediate\write18{nohup python3 -c 'from talk2stat.talk2stat import server,client; server("./","matlab") if not client("./","matlab","``` ```") else print("server is already running")' &}}
+ \fi
}
\DeclareOption{stopserver}{
@@ -76,6 +89,7 @@
\ProcessOptions*
+\usepackage{morewrites} % allow more than 16 \write
\usepackage[many]{tcolorbox} % to put boxes around text
\usepackage{xcolor} % for highlighting
\usepackage[utf8x]{inputenc}
@@ -132,19 +146,24 @@
% Args #3 and #4 are the first and last line to show (optional).
\NewDocumentCommand{\showCode}{m m O{} O{}}{% \-\\
- \ifthenelse{\isempty{#3}}{
+ \IfFileExists{#2}{
+ \ifthenelse{\isempty{#3}}{
\ifminted
\inputminted[fontsize=\footnotesize,linenos, frame=single, bgcolor=bg, breaklines=true]{#1}{#2}
\else
- \VerbatimInput[fontsize=\footnotesize,frame=single,breaklines]{#2}
+ \VerbatimInput[fontsize=\footnotesize,linenos=true,frame=single,breaklines]{#2}
\fi
}{
\ifminted
\inputminted[fontsize=\footnotesize,linenos,frame=single, bgcolor=bg, breaklines=true, firstline=#3, lastline=#4, firstnumber=1]{#1}{#2}
\else
- \VerbatimInput[fontsize=\footnotesize,frame=single,breaklines, firstline=#3, lastline=#4, firstnumber=1]{#2}
+ \VerbatimInput[fontsize=\footnotesize,linenos=true,frame=single,breaklines, firstline=#3, lastline=#4, firstnumber=1]{#2}
\fi
+ }
+ }{
+ \textcolor{red}{\textbf{showCode: File #2 does not exist!}}
}
+
}
@@ -158,6 +177,7 @@
% to \runcode; run = force the code to run; cache or anything else =
% use cache)
\NewDocumentCommand{\runExtCode}{m m m O{}}{
+ \IfFileExists{#2}{
\stepcounter{codeOutput}
\ifthenelse{\isempty{#3}}
{ \setvalue{\tmpname}{tmp/\jobname_tmp\thecodeOutput.tex} }
@@ -170,6 +190,9 @@
}{
\ifstrequal{#4}{run}{\immediate\write18{#1 #2 > \tmpname }}{}
}
+ }{
+ \textcolor{red}{\textbf{runExtCode: File #2 does not exist!}}
+ }
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -200,9 +223,15 @@
{\input{\tmpname}}}}
% if code execution disabled, and no cache:
{\begin{tcolorbox}
- Code execution disabled (cache not available).
+ \textbf{Output file \tmpname~ not found}. Check the file name (it may be that the file name was given with the suffix .tex. If so, remove it). If the file name is correct the problem may be because code execution is disabled and no cache is available. If so, force the code to run again (using the [run] option).
\end{tcolorbox}}}
+
+\NewDocumentCommand{\checkZeroBytes}{m}{
+ \immediate\write18{python3 -c 'import os; print("ZERO BYTES IN OUTPUT", file=open("#1", "a")) if os.path.getsize("#1") == 0 else True'}
+}
+
+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \inln{Arg #1}{Arg #2}[Arg #3] is used to execute short source code
% and embed resulting output.
@@ -223,7 +252,7 @@
\immediate\write18{#1 \tmpname.txt > \tmpname.tex}\unskip
\fi}
\IfFileExists{\tmpname.tex}
- {\unskip
+ {\checkZeroBytes{\tmpname.tex}\unskip
\ifstrequal{#3}{vbox}
{\begin{tcolorbox}[breakable,colback=red!5!white,colframe=red!75!black]
\ifminted\unskip
@@ -233,7 +262,7 @@
\fi
\end{tcolorbox}}
{\unskip\input{\tmpname.tex}\unskip}}
- {\textcolor{red}{notrun}}}
+ {\textcolor{red}{\textbf{Output file \tmpname~ not found}}}}
\NewDocumentCommand{\runR}{O{python3 -c 'from talk2stat.talk2stat import client; client("./","R","#2")'} m m O{}}
{