summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/runcode/runcode.sty
blob: 6737fd213f601797a98562333b4fa5dc6feb6533 (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
%
%  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!

\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}

% 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 = 300}
    \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 = 300}
    \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 = 300}
    \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{stopserver}{
  \AtEndDocument{
  %% stop the server when the pdf compilation is done.
    \immediate\write18{python3 -c 'from talk2stat.talk2stat import client; client("./","R","QUIT")'}
    \immediate\write18{python3 -c 'from talk2stat.talk2stat import client; client("./","julia","QUIT")'}
    \immediate\write18{python3 -c 'from talk2stat.talk2stat import client; client("./","matlab","QUIT")'}
}}

\ProcessOptions*

\usepackage{morewrites}               % allow more than 16 \write
\usepackage[many]{tcolorbox}            % to put boxes around text
\usepackage{xcolor}               % for highlighting
\usepackage[utf8x]{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}
\else
\usepackage{fvextra}
\fi

\immediate\write18{mkdir 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[fontsize=\footnotesize,linenos, frame=single, bgcolor=bg, breaklines=true]{#1}{#2}
    \else
    \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,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}[breakable,colback=red!5!white,colframe=red!75!black]
        \ifminted\unskip
 \inputminted[fontsize=\footnotesize,breaklines=true]{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}
      \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
  \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}
  \IfFileExists{\tmpname.tex}
  {\checkZeroBytes{\tmpname.tex}\unskip
    \ifstrequal{#3}{vbox}
        {\begin{tcolorbox}[breakable,colback=red!5!white,colframe=red!75!black]
        \ifminted\unskip
 \inputminted[fontsize=\footnotesize,breaklines=true]{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]}}

\endinput