diff options
Diffstat (limited to 'macros/latex/contrib/runcode')
-rw-r--r-- | macros/latex/contrib/runcode/runcode.pdf | bin | 169426 -> 170764 bytes | |||
-rw-r--r-- | macros/latex/contrib/runcode/runcode.sty | 55 | ||||
-rw-r--r-- | macros/latex/contrib/runcode/runcode.tex | 16 |
3 files changed, 53 insertions, 18 deletions
diff --git a/macros/latex/contrib/runcode/runcode.pdf b/macros/latex/contrib/runcode/runcode.pdf Binary files differindex 1a8d909f4c..13fcdea4da 100644 --- a/macros/latex/contrib/runcode/runcode.pdf +++ b/macros/latex/contrib/runcode/runcode.pdf 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{}} { diff --git a/macros/latex/contrib/runcode/runcode.tex b/macros/latex/contrib/runcode/runcode.tex index 5d99475739..977a41bc24 100644 --- a/macros/latex/contrib/runcode/runcode.tex +++ b/macros/latex/contrib/runcode/runcode.tex @@ -1,6 +1,6 @@ - % LaTeX Package: runcode v1.0 2020/10/04 + % LaTeX Package: runcode v1.1 2021/04/17 % - % Copyright (C) 2020 by Haim Bar and HaiYing Wang + % Copyright (C) 2021 by Haim Bar and HaiYing Wang % % This file may be distributed and/or modified under the % conditions of the LaTeX Project Public License, either @@ -15,9 +15,9 @@ \documentclass{ltxdoc} \usepackage[hyphens]{url} %\usepackage{runcode} -\ProvidesPackage{runcode}[2020/10/04 runcode v1.0] +\ProvidesPackage{runcode}[2021/04/17 runcode v1.1] \begin{document} - \title{The \textsf{runcode} package\thanks{This document corresponds to \textsf{runcode}~v1.0, dated~2020/10/04.}} + \title{The \textsf{runcode} package\thanks{This document corresponds to \textsf{runcode}~v1.1, dated~2021/04/17.}} \author{Haim Bar and HaiYing Wang \\ \texttt{haim.bar@uconn.edu}, \texttt{haiying.wang@uconn.edu}} \maketitle @@ -25,7 +25,7 @@ It is recommended to use this package in the server-mode together with the Python talk2stat package. Currently, the server-mode supports Julia, MatLab, Python, and R. More languages will be added. -For more details and usage examples, refer to the package's github repository, at +For more details and usage examples and troubleshooting, refer to the package's github repository, at \url{https://github.com/Ossifragus/runcode}. } @@ -49,6 +49,7 @@ Available options are: \item \texttt{run}: run source code, and store results in cache files. \item \texttt{cache}: use cached results. \item \texttt{stopserver}: stop the \textit{talk2stat} server(s) when the pdf compilation is done. +\item \texttt{nohup}: when using the server-mode, 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. \item \texttt{nominted}: use the \textit{fvextra} package [\url{https://ctan.org/pkg/fvextra}] instead of the \textit{minted} package [\url{https://ctan.org/pkg/minted}] to show code (\textit{fvextra} does not require Python's pygments package [\url{https://pygments.org/}], but it does not provide syntax highlights). \end{itemize} * Requires the Python package \textit{talk2stat} to be installed. @@ -140,6 +141,11 @@ Available options are: \end{itemize} +\section{Revisions} +\begin{itemize} +\item v1.1, April 17, 2021: Added a nohup option; improved error handling (missing code files, zero bytes in output files.) +\end{itemize} + % \section{Contributing} We welcome your contributions to this package by opening issues on GitHub and/or making a pull request. We also appreciate more example documents written using \textsf{runcode}. |