Debian Codegraph @

texmf-dist/doc/latex/runcode/runcode.tex

raw
1 % LaTeX Package: runcode 2025/01/17 v2.4
2 %
3 % Copyright (C) 2020-2025 by Haim Bar and HaiYing Wang
4 %
5 % This file may be distributed and/or modified under the
6 % conditions of the LaTeX Project Public License, either
7 % version 1.3c of this license or (at your option) any later
8 % version. The latest version of this license is in:
9 %
10 % http://www.latex-project.org/lppl.txt
11 %
12 % and version 1.3c or later is part of all distributions of
13 % LaTeX version 2005/12/01 or later.
14
15 \documentclass{ltxdoc}
16 \usepackage[utf8]{inputenc}
17 \usepackage[T1]{fontenc}
18 \usepackage{graphicx}
19 \usepackage{longtable}
20 \usepackage{wrapfig}
21 \usepackage{rotating}
22 \usepackage[normalem]{ulem}
23 \usepackage{amsmath}
24 \usepackage{amssymb}
25 \usepackage{capt-of}
26 \usepackage{hyperref}
27 \usepackage{minted}
28 \author{Haim Bar and HaiYing Wang \\[email protected], [email protected]}
29 \date{\today}
30 \title{The \textbf{runcode} package}
31 \begin{document}
32
33 \maketitle
34 \begin{abstract}
35
36 \texttt{runcode} is a \LaTeX{} package that executes programming source codes (including
37 all command line tools) from \LaTeX{}, and embeds the results in the resulting pdf
38 file. Many programming languages can be easily used and any command-line
39 executable can be invoked when preparing the pdf file from a tex file. \texttt{runcode}
40 is also available on \href{https://ctan.org/pkg/runcode}{CTAN}.
41
42 It is recommended to use this package in the server mode together with the
43 \href{https://www.python.org/}{Python} \href{https://pypi.org/project/talk2stat/}{talk2stat} package. Currently, the server mode supports \href{https://julialang.org/}{Julia}, \href{https://www.mathworks.com/products/matlab.html}{MatLab},
44 \href{https://www.python.org/}{Python}, and \href{https://www.r-project.org/}{R}. More languages will be added.
45
46 For more details and usage examples and troubleshooting, refer to the
47 package’s github repository, at \url{https://github.com/Ossifragus/runcode}.
48
49 \end{abstract}
50
51 \section{Installation}
52 \label{sec:org7b41ef7}
53 You can simply put the runcode.sty file in the \LaTeX{} project folder.
54
55 The server mode requires the \href{https://pypi.org/project/talk2stat/}{talk2stat} package, and some chunk command (e.g.,
56 \texttt{\textbackslash{}showChunk}) requires the \href{https://pypi.org/project/advance-touch/}{advance-touch} package. To install them from the
57 command line, use:
58
59 \begin{verbatim}
60 pip3 install talk2stat advance-touch
61 \end{verbatim}
62
63 \textbf{Note}: \texttt{runcode} requires to enable the \texttt{shell-escape} option when
64 compiling a \LaTeX{} document.
65
66
67 \section{Usage}
68 \label{sec:org5034faf}
69 \subsection{Load the package:}
70 \label{sec:orge3286de}
71 \begin{minted}[]{latex}
72 \usepackage[options]{runcode}
73 \end{minted}
74
75 Available options are:
76
77 \begin{itemize}
78 \item \texttt{cache}: use cached results.
79
80 \item \texttt{fvextra}: use the \href{https://ctan.org/pkg/fvextra}{fvextra} package to show code.
81
82 \item \texttt{julia}: start server for \href{https://julialang.org/}{Julia} (requires \href{https://pypi.org/project/talk2stat/}{talk2stat}).
83
84 \item \texttt{listings}: use the \href{https://ctan.org/pkg/listings?lang=en}{listings} package to show code.
85
86 \item \texttt{matlab}: start server for \href{https://www.mathworks.com/products/matlab.html}{MatLab} (requires \href{https://pypi.org/project/talk2stat/}{talk2stat}).
87
88 \item \texttt{minted}: use the \href{https://ctan.org/pkg/minted}{minted} package to show code (requires the \href{https://pygments.org/}{pygments} package).
89 This is the default option.
90
91 \item \texttt{nominted}: use the \href{https://ctan.org/pkg/fvextra}{fvextra} package
92 instead of the \href{https://ctan.org/pkg/minted}{minted} package to show
93 code (this does not require the \href{https://pygments.org/}{pygments}
94 package, but it does not provide syntax highlights).
95
96 \item \texttt{nohup}: use the \texttt{nohup} command when starting a server. When using
97 the server-mode, some editors terminate all child processes after
98 \LaTeX{} compiling such as Emacs with Auctex. This option set the
99 variable notnohup to be false, and the server will not be terminated
100 by the parent process. \textbf{This option has to be declared before
101 declaring any language}, e.g., \texttt{[nohup, R]} works but \texttt{[R, nohup]}
102 does not work.
103
104 \item \texttt{python}: start server for \href{https://www.python.org/}{Python}
105 (requires \href{https://pypi.org/project/talk2stat/}{talk2stat}).
106
107 \item \texttt{run}: run source code.
108
109 \item \texttt{R}: start server for \href{https://www.r-project.org/}{R} (requires
110 \href{https://pypi.org/project/talk2stat/}{talk2stat}).
111
112 \item \texttt{stopserver}: stop the server(s) when the pdf compilation is done.
113 \end{itemize}
114
115 \textbf{Note}: If \href{https://ctan.org/pkg/minted}{minted} is used, the style of
116 the code block is controlled through the minted package,
117 \href{https://github.com/Ossifragus/runcode/blob/master/examples/MontyHall/MontyHall.tex\#L3-L4}{e.g.:}
118
119 \begin{minted}[]{latex}
120 \setminted[julia]{linenos, frame=single, bgcolor=bg, breaklines=true}
121 \setminted[R]{linenos, frame=single, bgcolor=lightgray, breaklines=true}
122 \end{minted}
123
124 Similarly, \href{https://ctan.org/pkg/fvextra}{fvextra} and \href{https://ctan.org/pkg/listings?lang=en}{listings} packages can be customized through the \texttt{\textbackslash{}fvset}
125 and \texttt{\textbackslash{}lstset} commands, respectively, e.g.:
126
127 \begin{minted}[]{latex}
128 \fvset{fontsize=\small, linenos=true, frame=single}
129 \lstset{basicstyle=\large, frame=single}
130 \end{minted}
131
132 The outputs from executing codes are displayed in
133 \href{https://ctan.org/pkg/tcolorbox?lang=en}{tcolorbox}, so the style can
134 be customized with \texttt{\textbackslash{}tcbset},
135 \href{https://github.com/Ossifragus/runcode/blob/master/examples/MontyHall/MontyHall.tex\#L5}{e.g.:}
136
137 \begin{minted}[]{latex}
138 \tcbset{breakable,colback=red!5!white,colframe=red!75!black}
139 \end{minted}
140
141 \subsection{Basic commands:}
142 \label{sec:org496679c}
143 \begin{itemize}
144 \item \texttt{\textbackslash{}runExtCode\{Arg1\}\{Arg2\}\{Arg3\}[Arg4]} runs an external code.
145
146 \begin{itemize}
147 \item \texttt{Arg1} is the executable program.
148 \item \texttt{Arg2} is the source file name.
149 \item \texttt{Arg3} is the output file name (with an empty value, the counter \texttt{codeOutput} is
150 used).
151 \item \texttt{Arg4} controls whether to run the code. \texttt{Arg4} is optional with three possible
152 values.
153 \begin{itemize}
154 \item empty value or skipped: the value of the global Boolean variable \texttt{runcode}
155 is used;
156 \item \texttt{run}: the code will be executed;
157 \item \texttt{cache} (or anything else): use cached results; if the output file does not
158 exist, the cache option will be overridden and the code will run.
159 \end{itemize}
160 \end{itemize}
161
162 \item \texttt{\textbackslash{}showCode\{Arg1\}\{Arg2\}[Arg3][Arg4]} shows the source code, using
163 \href{https://ctan.org/pkg/minted}{minted} (requires \href{https://pygments.org/}{pygments}), \href{https://ctan.org/pkg/fvextra}{fvextra}, or \href{https://ctan.org/pkg/listings?lang=en}{listings}.
164
165 \begin{itemize}
166 \item \texttt{Arg1} is the programming language.
167 \item \texttt{Arg2} is the source file name.
168 \item \texttt{Arg3} is the first line to show (optional with a default value 1).
169 \item \texttt{Arg4} is the last line to show (optional with a default value of
170 the last line).
171 \end{itemize}
172
173 \item \texttt{\textbackslash{}includeOutput\{Arg1\}[Arg2]} is used to embed the output from executed
174 code.
175
176 \begin{itemize}
177 \item \texttt{Arg1} is the output file name, and it needs to have the same value
178 as that of \texttt{Arg3} in \texttt{\textbackslash{}runExtCode}. If an empty value is given to
179 \texttt{Arg1}, the counter \texttt{codeOutput} is used.
180 \item \texttt{Arg2} is optional and it controls the type of output with a default
181 value \texttt{vbox}.
182 \begin{itemize}
183 \item \texttt{vbox} (or skipped) = verbatim in a box;
184 \item \texttt{tex} = pure latex;
185 \item \texttt{inline} = embed result in text.
186 \end{itemize}
187 \end{itemize}
188
189 \item \texttt{\textbackslash{}inln\{Arg1\}\{Arg2\}[Arg3][Arg4]} is designed for simple calculations; it runs
190 one command (or a short batch) and displays the output within the
191 text.
192
193 \begin{itemize}
194 \item \texttt{Arg1} is the executable program or programming language.
195 \item \texttt{Arg2} is the source code.
196 \item \texttt{Arg3} is the output file name (optional);
197 \item \texttt{Arg4} is the output type and controls whether to run the code.
198 \begin{itemize}
199 \item \texttt{inline} (or skipped or with empty value): embed result in text;
200 \item \texttt{vbox}: verbatim in a box;
201 \item \texttt{cache} or appending \texttt{.cache} to the argument (e.g., \texttt{vbox.cache}) AND the
202 output file in \texttt{Arg3} exists: the cached result will be used and the code
203 will not run.
204 \end{itemize}
205 \end{itemize}
206
207 \item \texttt{\textbackslash{}showChunk\{Arg1\}\{Arg2\}[Arg3][Arg4]} prints a selected chunk from source
208 code. The chunk is identified in the source code by two strings that define
209 the beginning and end of the chunk. The default beginning is \texttt{label===<name>}
210 where \texttt{<name>} should be a unique user-defined chunk ID. The default end marker is
211 \texttt{===end}. In the code, these markers should appear after a comment character, so
212 that the code will run.
213
214 \begin{itemize}
215 \item \texttt{Arg1} is the programming language.
216 \item \texttt{Arg2} is the source file name.
217 \item \texttt{Arg3} is is the chunk identifier.
218 \item \texttt{Arg4} and \texttt{Arg5} are the beginning and end markings of a chunk (optional with
219 default \texttt{label==} and \texttt{===end}.
220 \end{itemize}
221 \end{itemize}
222
223
224
225 \subsection{Extended commands:}
226 \label{sec:orgf1638ce}
227 \begin{itemize}
228 \item \texttt{\textbackslash{}runCodeIncOut\{Arg1\}\{Arg2\}[Arg3][Arg4][Arg5]} runs an external code and
229 embeds the output. This is a combination of \texttt{\textbackslash{}runExtCode} and \texttt{\textbackslash{}includeOutput}.
230 \begin{itemize}
231 \item \texttt{Arg1} is the executable program.
232 \item \texttt{Arg2} is the source file name.
233 \item \texttt{Arg3} (optional) controls whether to run the code. Its functionality is the
234 same as that of \texttt{Arg4} of \texttt{\textbackslash{}runExtCode}.
235 \item \texttt{Arg4} (optional) is the output file name. Its functionality is the same as
236 that of \texttt{Arg3} of \texttt{\textbackslash{}runExtCode}.
237 \item \texttt{Arg5} (optional) controls the type of output with a default value \texttt{vbox}. Its
238 functionality is the same as that of \texttt{Arg3} of \texttt{\textbackslash{}includeOutput}.
239 \end{itemize}
240 \end{itemize}
241
242 \subsection{Language specific shortcuts:}
243 \label{sec:org8195ea0}
244 Replace \texttt{LANG} with \texttt{Julia}, \texttt{MatLab}, \texttt{Python}, or \texttt{R}, for the \href{https://julialang.org/}{Julia}, \href{https://www.mathworks.com/products/matlab.html}{MatLab}, \href{https://www.python.org/}{Python}, \href{https://www.r-project.org/}{R}
245 language, respectively, for the following commands.
246
247 \begin{itemize}
248 \item \texttt{\textbackslash{}runLANG[Arg1]\{Arg2\}\{Arg3\}[Arg4]} runs an external \texttt{LANG} code file.
249 \begin{itemize}
250 \item \texttt{Arg1} is optional and uses \href{https://pypi.org/project/talk2stat/}{talk2stat}'s \texttt{LANG} server by default.
251 \item \texttt{Arg2}, \texttt{Arg3}, and \texttt{Arg4} have the same effects as those of the basic command
252 \texttt{\textbackslash{}runExtCode}.
253 \end{itemize}
254 \item \texttt{\textbackslash{}runLANGIncOut[Arg1]\{Arg2\}[Arg3][Arg4][Arg5]} runs an external \texttt{LANG} code
255 file and embeds the output.
256 \begin{itemize}
257 \item \texttt{Arg1} is optional and uses \href{https://pypi.org/project/talk2stat/}{talk2stat}'s \texttt{LANG} server by default.
258 \item \texttt{Arg2}, \texttt{Arg3}, \texttt{Arg4}, and \texttt{Arg5} have the same effects as those of the command
259 \texttt{\textbackslash{}runCodeIncOut}.
260 \end{itemize}
261 \item \texttt{\textbackslash{}inlnLANG[Arg1]\{Arg2\}[Arg3][Arg4]} runs \texttt{LANG} source code (\texttt{Arg2}) and displays
262 the output in line.
263 \begin{itemize}
264 \item \texttt{Arg1} is optional and uses the \texttt{LANG} server by default.
265 \item \texttt{Arg2} is the \texttt{LANG} source code to run. If the \texttt{LANG} source code is wrapped
266 between "\texttt{```}" on both sides (as in the markdown grammar), then it will be
267 implemented directly; otherwise the code will be written to a file on the
268 disk and then be called.
269 \item \texttt{Arg3} and \texttt{Arg4} have the same effects as those of the basic command \texttt{\textbackslash{}inln}.
270 \end{itemize}
271
272 \item \texttt{\textbackslash{}runLANGChunk[Arg1]\{Arg2\}\{Arg3\}[Arg4][Arg5][Arg6]} runs a selected chunk from
273 an external \texttt{LANG} code file and embeds the output.
274 \begin{itemize}
275 \item \texttt{Arg1} (optional ) uses \href{https://pypi.org/project/talk2stat/}{talk2stat}'s \texttt{LANG} server by default.
276 \item \texttt{Arg2} is the source file name.
277 \item \texttt{Arg3} is is the chunk identifier.
278 \item \texttt{Arg4} (optional) controls whether to run the code.
279 \item \texttt{Arg5} (optional) is the output file name.
280 \item \texttt{Arg6} (optional) controls the type of output with a default value \texttt{vbox}.
281 \end{itemize}
282 \end{itemize}
283
284 For example,
285 \begin{minted}[]{latex}
286 \runR{code/MontyHall_1.R}{montyhall-R1}
287 \runRIncOut{code/MontyHall_1.R}[][montyhall-R1]
288 \end{minted}
289
290 Specifically for \href{https://www.python.org/}{Python},
291
292 \begin{itemize}
293 \item \texttt{\textbackslash{}runPythonBatch[Arg1][Arg2]} runs an external \href{https://www.python.org/}{Python} code file in batch mode
294 (without a server running). Python (at least currently), unlike the other
295 languages we use, does not have an option to save and restore a session, which
296 means that once a Python session ends, the working environement (variable,
297 functions) is deleted. In order to allow a batch-mode in Python, we
298 implemented such capability. It requires the \href{https://pypi.org/project/dill/}{dill} module, which has to be
299 installed via \texttt{pip3 install dill}.
300 \begin{itemize}
301 \item \texttt{Arg1} is the \href{https://www.python.org/}{Python} source file name,
302 \item \texttt{Arg2} is the output file name.
303 \end{itemize}
304 \end{itemize}
305
306
307 \section{Revisions}
308 \label{sec:orge3b69ca}
309 \begin{itemize}
310 \item v2.4, January 17, 2025: 1. put file names within \$\$ for more robust
311 warnings; 2. use the python \href{https://pypi.org/project/advance-touch/}{advance-touch} package to create sub-folders for
312 chunk related commands.
313 \item v2.3, January 12, 2024: two bug fixes (1. removed an extra space after
314 \texttt{\textbackslash{}inlnX}; 2. when an underscore appeared inside R code, the tex file would
315 compile with an error).
316 \item v2.2, September 8, 2023: add \texttt{\textbackslash{}showChunk} basic command and \texttt{\textbackslash{}runLANGChunk}
317 commands for multiple languages.
318 \item v2.1, June 30, 2023: detokenize code which is passed to \texttt{\textbackslash{}inln}. This is
319 necessary when the code contains special latex characters like backslash.
320 These characters are escaped by latex when they are passed to macros as arguments,
321 which, in the case of \texttt{\textbackslash{}inln}, alters the correct syntax and causes execution errors.
322 Detokenizing ensures that the code is passed as-is to R/Julia/MatLab/Python.
323 We thank \href{https://github.com/kiryph}{kiryph} for reporting the issue.
324 \item v2.0, June 23, 2023: add \texttt{\textbackslash{}runCodeIncOut} command that runs an external code and
325 embeds the output and andd some language specific shortcuts; update
326 \texttt{\textbackslash{}runExtCode} so that if the output file does not exist, the \texttt{cache} option will
327 be overridden and the code will run; update \texttt{\textbackslash{}inln} so that its \texttt{Arg4} accept
328 \texttt{cache} or appending \texttt{.cache} to use the cached result. We thank \href{https://github.com/kiryph}{kiryph} for
329 suggesting these features.
330 \item v1.9, June 13, 2023: update \texttt{\textbackslash{}inln} command; the optional \texttt{Arg3} is the output
331 file name and the optional \texttt{Arg4} is the output type.
332 \item v1.8, January 18, 2023, add support to \href{https://ctan.org/pkg/listings?lang=en}{listings.}
333 \item v1.7, August 20, 2022: changed the tmp/ folder to generated/ in order to
334 conform with CTAN suggestions; renamed the troubleshooting file.
335 \item v1.6, August 10, 2022: stop only configured/running servers; a new
336 reducedspace option - some document classes put more space after the code box;
337 changed the default timeout of servers to 60 seconds; expanded the
338 troubleshooting document. New examples are now available on GitHub, including
339 how to collaborate with people who use Overleaf.
340 \item v1.5, July 23, 2022: Removed the utf8x option when loading inputenc due to a
341 conflict with hyperref.
342 \item v1.4, July 18, 2022: Fixed a bug in the cache mode.
343 \item v1.3, May 14, 2022: Removed the hard-coded minted options.
344 \item v1.2, May 3, 2022: Added python options (server and batch).
345 \item v1.1, April 17, 2021: Added a nohup option; improved error handling (missing
346 code files, zero bytes in output files.)
347 \end{itemize}
348
349 \section{Contributing}
350 \label{sec:orged22cfb}
351 We welcome your contributions to this package by opening issues on
352 GitHub and/or making a pull request. We also appreciate more example
353 documents written using \texttt{runcode}.
354
355
356 \textbf{Citing} \texttt{runcode}:
357 Haim Bar and HaiYing Wang (2021). \href{https://jds-online.org/journal/JDS/article/103/info}{Reproducible Science with \LaTeX{}},
358 \emph{Journal of Data Science} 2021; 19, no. 1, 111-125, DOI 10.6339/21-JDS998
359 \end{document}