summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/runcode/runcode.sty
blob: 27202b469addbc8dbc4eeb942724e40afd7942eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
%
%  Haim Bar      haim.bar@uconn.edu
%  HaiYing Wang  haiying.wang@uconn.edu
%
%  This package is based on an ongoing work by Haim Bar and HaiYing Wang, and comments and questions are welcome!

\ProvidesPackage{runcode}[2022/08/10 runcode v1.6]

\newif\ifruncode
% Change to \runcodefalse if you want to suspend code execution
\runcodetrue
\DeclareOption{run}{\runcodetrue}
\DeclareOption{cache}{\runcodefalse}

\newif\ifminted
\mintedtrue
\DeclareOption{nominted}{\mintedfalse}

\newif\ifreducedspace
\reducedspacefalse
\DeclareOption{reducedspace}{\reducedspacetrue}


% 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}{}{
    \newwrite\tempfile
    \immediate\openout\tempfile=R.config
    \immediate\write\tempfile{[SERVERCONFIG]}
    \immediate\write\tempfile{PORT = 65432}
    \immediate\write\tempfile{DEBUGFILE = Rdebug.txt}
    \immediate\write\tempfile{PIPETIMEOUT = 60}
    \immediate\closeout\tempfile
  }
  % Start the server. Need to run just once. Can comment out after the first
  % compilation, but remember to terminate the server
  \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}{
  % create a configuration file for R server if it does not exist.
  \IfFileExists{julia.config}{}{
    \newwrite\tempfile
    \immediate\openout\tempfile=julia.config
    \immediate\write\tempfile{[SERVERCONFIG]}
    \immediate\write\tempfile{PORT = 65431}
    \immediate\write\tempfile{DEBUGFILE = juliadebug.txt}
    \immediate\write\tempfile{PIPETIMEOUT = 60}
    \immediate\closeout\tempfile
  }
  % Start the server. Need to run just once. Can comment out after the first
  % compilation, but remember to terminate the server
  \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}{
  % create a configuration file for R server if it does not exist.
  \IfFileExists{matlab.config}{}{
    \newwrite\tempfile
    \immediate\openout\tempfile=matlab.config
    \immediate\write\tempfile{[SERVERCONFIG]}
    \immediate\write\tempfile{PORT = 65430}
    \immediate\write\tempfile{DEBUGFILE = matlabdebug.txt}
    \immediate\write\tempfile{PIPETIMEOUT = 60}
    \immediate\closeout\tempfile
  }
  % Start the server. Need to run just once. Can comment out after the first
  % compilation, but remember to terminate the server
  \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{python}{
  % create a configuration file for Python server if it does not exist.
  \IfFileExists{python.config}{}{
    \newwrite\tempfile
    \immediate\openout\tempfile=python.config
    \immediate\write\tempfile{[SERVERCONFIG]}
    \immediate\write\tempfile{PORT = 65433}
    \immediate\write\tempfile{DEBUGFILE = pythondebug.txt}
    \immediate\write\tempfile{PIPETIMEOUT = 60}
    \immediate\closeout\tempfile
  }
  % Start the server. Need to run just once. Can comment out after the first
  % compilation, but remember to terminate the server
  \ifnotnohup
  {\immediate\write18{python3 -c 'from talk2stat.talk2stat import server,client; server("./","python") if not client("./","python","``` ```") else print("server is already running")'}}
  \else
  {\immediate\write18{nohup python3 -c 'from talk2stat.talk2stat import server,client; server("./","python") if not client("./","python","``` ```") else print("server is already running")' &}}
  \fi  
}

\DeclareOption{stopserver}{
  \AtEndDocument{
  %% stop the server when the pdf compilation is done.
  \IfFileExists{R.config}{
      \immediate\write18{python3 -c 'from talk2stat.talk2stat import client; client("./","R","QUIT")'}
    }{}
    \IfFileExists{julia.config}{
      \immediate\write18{python3 -c 'from talk2stat.talk2stat import client; client("./","julia","QUIT")'}
    }{}
    \IfFileExists{matlab.config}{
      \immediate\write18{python3 -c 'from talk2stat.talk2stat import client; client("./","matlab","QUIT")'}
    }{}
    \IfFileExists{python.config}{
      \immediate\write18{python3 -c 'from talk2stat.talk2stat import client; client("./","python","QUIT")'}
    }{}
}}

\ProcessOptions*

\usepackage{morewrites}               % allow more than 16 \write
\usepackage[many]{tcolorbox}            % to put boxes around text
\tcbset{colframe=blue!25,colback=blue!10} % default colors for box output
\usepackage{xcolor}               % for highlighting
\usepackage{inputenc}
\usepackage{textgreek}
\usepackage{filecontents}
\usepackage{xifthen}
\usepackage{xparse}
\usepackage{xstring}

% Use minted if it is loaded; otherwise use fvextra
\ifminted
\usepackage[cache=false]{minted}
\setminted{fontsize=\footnotesize,breaklines=true} % minted default
\else
\usepackage{fvextra}
\fi

\immediate\write18{mkdir -p tmp}

\definecolor{bg}{rgb}{0.95,0.95,0.95} % code block background color

\newcounter{codelisting}
\renewcommand{\thecodelisting}{\arabic{codelisting}}
\newenvironment{codelisting}[1][]{
 \par\vspace{\baselineskip}\noindent
  \refstepcounter{codelisting}
  \begin{ttfamily}
   \noindent \textbf{Listing~\thecodelisting. #1}
}
{
 \end{ttfamily}
  \noindent\ignorespacesafterend
 }  

% counter for code output temporary file names
\newcounter{codeOutput}
\setcounter{codeOutput}{0}

% a command to set the temporary file name for code output
\newcommand{\setvalue}[2]{
    \ifdefined #1
        \renewcommand{#1}{#2}
    \else
        \newcommand{#1}{#2}
    \fi
}
\setvalue{\tmpname}{} % initialize with a blank



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
% \showCode prints the source code, using minted for a pretty layout
% Arg #1 is the programming language,
% Arg #2 is the source file name,
% Args #3 and #4 are the first and last line to show (optional).

\NewDocumentCommand{\showCode}{m m O{} O{}}{% \-\\
  \IfFileExists{#2}{
    \ifthenelse{\isempty{#3}}{
    \ifminted
    \inputminted{#1}{#2}
    \ifreducedspace
      \vspace{-\medskipamount}
      \vspace{-\baselineskip}
    \fi
    \else
    \VerbatimInput[fontsize=\footnotesize,linenos=true,frame=single,breaklines]{#2}
    \fi
  }{
    \ifminted
    \inputminted[firstline=#3, lastline=#4, firstnumber=1]{#1}{#2}
    \ifreducedspace
      \vspace{-\medskipamount}
      \vspace{-\baselineskip}
    \fi
    \else
    \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!}}
  }

}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \runExtCode runs an external code.
% Arg #1 is the executable program,
% Arg #2 is the source file name,
% Arg #3 is the output file name (optional - if not given, the counter 
%        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)
\NewDocumentCommand{\runExtCode}{m m m O{}}{
  \IfFileExists{#2}{
  \stepcounter{codeOutput}
  \ifthenelse{\isempty{#3}}
    { \setvalue{\tmpname}{tmp/\jobname_tmp\thecodeOutput.tex} }
    { \setvalue{\tmpname}{tmp/#3.tex} }
  % toggle \runcode above if you want to enable/disable code execution
    \ifthenelse{\isempty{#4}}
      { \ifruncode
        \immediate\write18{#1 #2 > \tmpname }
        \fi
      }{
        \ifstrequal{#4}{run}{\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 
%        codeOutput is used).
% Arg #2 is the optional type output with default "vbox"
%         (vbox = verbatim in a box, tex = pure latex, or inline)

\NewDocumentCommand{\includeOutput}{m O{vbox}}{\-
  \ifthenelse{\isempty{#1}}
  {\setvalue{\tmpname}{tmp/\jobname_tmp\thecodeOutput.tex}}
  {\unskip\setvalue{\tmpname}{tmp/#1.tex}\unskip}\unskip
  % even if the code is not executed, but we ran it before, we can use a 
  % cached version if it exists.
  \IfFileExists{\tmpname}
  {
    \ifstrequal{#2}{vbox}
    {\begin{tcolorbox}
        \ifminted\unskip
          \inputminted{text}{\tmpname}\unskip
        \else\unskip
        \VerbatimInput[fontsize=\footnotesize,breaklines]{\tmpname}\unskip
        \fi
      \end{tcolorbox}}
    {\unskip\ifstrequal{#2}{inline}
      {\unskip\input{\tmpname}\unskip}
      {\input{\tmpname}}}}
  % if code execution disabled, and no cache:
  {\begin{tcolorbox}[colback=red!5!white,colframe=red!75!black]
      \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.
% Arg #1 is the executable program;
% Arg #2 is the source code
% Arg #3 is the type output (if skipped or with empty value the default type is
%        inline; vbox = verbatim in a box);

\NewDocumentCommand{\inln}{m m O{inline}}{\-
  \stepcounter{codeOutput}\unskip
  \unskip\setvalue{\tmpname}{tmp/\jobname_inln\thecodeOutput}\unskip\unskip\unskip
  \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
      \immediate\openout\tempfile=\tmpname.txt
      \immediate\write\tempfile{#2}
      \immediate\closeout\tempfile
      \ifruncode
      \immediate\write18{#1 \tmpname.txt > \tmpname.tex}\unskip
    \fi}
  \fi % end cache mode
  \IfFileExists{\tmpname.tex}
  {\checkZeroBytes{\tmpname.tex}\unskip
    \ifstrequal{#3}{vbox}
        {\begin{tcolorbox}
        \ifminted\unskip
          \inputminted{text}{\tmpname.tex}\unskip
        \else\unskip
        \VerbatimInput[fontsize=\footnotesize,breaklines]{\tmpname.tex}\unskip
        \fi
      \end{tcolorbox}}
    {\unskip\input{\tmpname.tex}\unskip}}
    {\textcolor{red}{\textbf{Output file \tmpname~ not found}}}}

\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")'}
  \ifstrequal{#1}{\runcmd}
  {\runExtCode{#1}{}{#3}[#4]}
  {\runExtCode{#1}{#2}{#3}[#4]}
}

\NewDocumentCommand{\inlnR}{O{python3 -c 'from talk2stat.talk2stat import client; client("./","R","\tmpname.txt")'} m O{inline}}{\IfBeginWith{#2}{```}{\inln{python3 -c 'from talk2stat.talk2stat import client;client("./","R","#2")'}{#2}[#3]}{\inln{#1}{#2}[#3]}}

\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")'}
  \ifstrequal{#1}{\runcmd}
  {\runExtCode{#1}{}{#3}[#4]}
  {\runExtCode{#1}{#2}{#3}[#4]}
}

\NewDocumentCommand{\inlnJulia}{O{python3 -c 'from talk2stat.talk2stat import client; client("./","julia","\tmpname.txt")'} m O{inline}}{\IfBeginWith{#2}{```}{\inln{python3 -c 'from talk2stat.talk2stat import client;client("./","julia","#2")'}{#2}[#3]}{\inln{#1}{#2}[#3]}}

\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")'}
  \ifstrequal{#1}{\runcmd}
  {\runExtCode{#1}{}{#3}[#4]}
  {\runExtCode{#1}{#2}{#3}[#4]}
}

\NewDocumentCommand{\inlnMatlab}{O{python3 -c 'from talk2stat.talk2stat import client; client("./","matlab","\tmpname.txt")'} m O{inline}}{\IfBeginWith{#2}{```}{\inln{python3 -c 'from talk2stat.talk2stat import client;client("./","matlab","#2")'}{#2}[#3]}{\inln{#1}{#2}[#3]}}


\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")'}
  \ifstrequal{#1}{\runcmd}
  {\runExtCode{#1}{}{#3}[#4]}
  {\runExtCode{#1}{#2}{#3}[#4]}
}

\NewDocumentCommand{\inlnPython}{O{python3 -c 'from talk2stat.talk2stat import client; client("./","python","\tmpname.txt")'} m O{inline}}{\IfBeginWith{#2}{```}{\inln{python3 -c 'from talk2stat.talk2stat import client;client("./","python","#2")'}{#2}[#3]}{\inln{#1}{#2}[#3]}}


%%%%%%%%
% For Python batch mode, implement saving and restoring session
% Arg #1 is the source file name,
% Arg #2 is the output file name

\NewDocumentCommand{\runPythonBatch}{m m}{%\-\\
   \stepcounter{codeOutput}
   \ifthenelse{\isempty{#2}} % set the output file name
        { \setvalue{\tmpname}{tmp/\jobname_tmp\thecodeOutput.tex} }
        { \setvalue{\tmpname}{tmp/#2.tex} }

   \IfFileExists{#1}{
     \ifruncode
          \newwrite\tempfile
          \immediate\openout\tempfile=tmp/\jobname_#1
          \immediate\write\tempfile{import dill}
          \immediate\write\tempfile{from os.path import exists}
          \immediate\write\tempfile{if exists('tmp/session'):}
          \immediate\write\tempfile{    dill.load_session('tmp/session')}
          \immediate\write\tempfile{}
          \newread\infile
          \openin\infile=#1
          \begingroup\endlinechar=-1
          \loop\unless\ifeof\infile
             \read\infile to\fileline  % Read one line and store it into \fileline
             \immediate\write\tempfile{\unexpanded\expandafter{\fileline}}  % print the content to copy.txt
          \repeat
          \endgroup
          \closein\infile
          \immediate\write\tempfile{dill.dump_session('tmp/session')}
          \immediate\write\tempfile{}
          \immediate\closeout\tempfile
          \immediate\write18{python3  tmp/\jobname_#1 > \tmpname}
       \fi
     }
     {
        \immediate\write18{cat /dev/null > \tmpname}
        \textcolor{red}{\textbf{runPythonBatch: File #1 does not exist!}}
     }
}


\endinput