Debian Codegraph @

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

raw
1 \documentclass[a4paper,10pt]{article}
2 \usepackage[a4paper, total={6in, 10in}]{geometry}
3 %\usepackage[square,sort,comma]{natbib}
4 % \usepackage[R,stopserver]{runcode}
5 \usepackage[cache]{runcode}
6
7 \usepackage{caption}
8 %\usepackage{subcaption}
9 \usepackage{subfig}
10
11
12 \newcommand{\prob}[1] {\bigskip\noindent\textbf{\textcolor{orange}{Problem:}}\\#1\\\\}
13 \newcommand{\cause}[1] {\noindent\textit{\textcolor{blue}{Possible reason and solution:}}\\#1}
14 \newcommand{\cmnt}[1] {\bigskip\noindent\textit{Notes:} #1}
15
16 % Title Page
17 \title{The runcode package -- troubleshooting}
18 \author{Haim Bar and HaiYing Wang}
19
20
21 \begin{document}
22 \maketitle
23
24 When using the runcode package, you have to be aware of some usage rules, so this document attempts to anticipate all the possible user or system errors, and show how to interpret the output and fix the problems.
25
26 \subsection*{Before you start using runcode}
27
28 Before using the package make sure that you have Python (version 3.x) installed and in your path. Also, you must install the required \LaTeX~ packages:
29 morewrites, tcolorbox, xcolor, inputenc, textgreek, filecontents, xifthen, xparse, xstring, and fvextra. The minted package is optional but recommended.
30
31 If you try to compile this file from its tex source, you will get errors (because the purpose of this document is to demonstrate errors and how to fix them.) It is assumed that you are using a cloned runcode repository, so files used in this document are ones in the directory structure as it is stored on github.
32
33 The files generated by runcode live in the project's directory, so we assume the user has the necessary permissions to create, modify, delete files and subfolder.
34
35 For the server mode the folder must contain a configuration file for each statistical language used (R, julia, matlab, Python). If such files don't exist, runcode will create them with default values. Remember to check if the defaults work for you. For example, you may need to change the port number, or increase the timeout parameter.
36
37 \subsection*{Some possible errors and solutions}
38
39
40 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41 \prob{runcode functions are not executed.}
42 \cause\bgroup
43 Check if you enabled the \verb|shell-escape| option when the document is compiled. If not, you will see warnings in the project's log file. For example:
44 \begin{Verbatim}
45 Package ifplatform Warning:
46 shell escape is disabled, so I can only detect \ifwindows.
47 ! Package minted Error: You must invoke LaTeX with the -shell-escape flag.
48 \end{Verbatim}
49 \egroup
50
51 \cause\bgroup
52 Check if the command-line tools you invoke from runcode are installed, and in your path (e.g., R, Julia, Matlab, Python).
53 \egroup
54
55 \cmnt\bgroup
56 The runcode package can call any command-line function when it is used in `batch-mode'. That is, when the command-line tool is called separately each time a computation is performed from within the tex document (upon compilation). When using R, Julia, Python, or Matlab, the user can maintain a continuous session to the corresponding command-line tool. This saves initialization time, and allows to keep a session's history, for performing steps sequentially and efficiently. This is the recommended way to use runcode with R, Julia, Python, and Matlab.
57 At the present time, no other languages are supported for `server-mode' operation.
58 \egroup
59
60
61 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
62 \prob{Code highlighting is not working properly.}
63 \cause\bgroup
64 When code is included in the manuscript, it is done via the \verb|\showCode| command. By default, the code-highlighting it done via the minted package. If code highlighting doesn't work, check if the minted package is installed properly. Python (3.x) also has to be installed, and also the Pygments package (which has to be installed via pip3). If you have trouble with the installation of minted, use the \verb|nominted| option when you include the runcode package. This will cause runcode to use the fvextra for code display, instead.
65 \egroup
66
67
68 \prob{Embedded code is not shown.}
69 \cause\bgroup
70 Check if you specified the source file correctly.
71 \verb|\showCode| prints the source code, using minted for a pretty layout. It takes 4 arguments.
72 Arg \#1 is the programming language,
73 Arg \#2 is the source file name,
74 Args \#3 and \#4 are the first and last line to show (optional).
75 If the source file name does not exist, you will get a red and bold error message. For example:
76 \begin{verbatim}\showCode{R}{Sim23.R}
77 \end{verbatim}
78 \showCode{R}{Sim23.R}
79 \noindent
80 In contrast, when the file exists, as in this example
81 \begin{verbatim}\showCode{R}{paper/supplement/Code/code1.R}
82 \end{verbatim}
83 the file will be shown correctly:
84 \showCode{R}{paper/supplement/Code/code1.R}
85 \egroup
86
87 \cause\bgroup
88 If the programming language is misspecified or not recognized by minted or fvextra, the code highlighting may not be shown correctly.
89 \begin{verbatim}\showCode{matlab}{paper/supplement/Code/code1.R}
90 \end{verbatim}
91 \showCode{matlab}{paper/supplement/Code/code1.R}
92 \egroup
93
94 \cause\bgroup
95 If the line number in Arg \#3 exceeds the actual number of lines in the code, the code box will be empty, and the \LaTeX~ compiler will show an error message in its log file (`Empty verbatim environment'). For example:
96 \begin{verbatim}\showCode{R}{paper/supplement/Code/code1.R}[6][8]
97 \end{verbatim}
98 % \showCode{R}{paper/supplement/Code/code1.R}[6][8]
99 % \vspace{-2em}
100 \noindent Some \LaTeX~ compilers will stop the compilation when they encounter `Empty verbatim environment' but will allow you to manually continue the compilation (and the generated pdf will contain an empty box with no code in it.)
101
102 \noindent If the number in Arg. \#4 is greater than the number of lines in the file, minted will show the code up to the last line (so this misspecification is harmless).
103 \begin{verbatim}\showCode{R}{paper/supplement/Code/code1.R}[4][8]
104 \end{verbatim}
105 \showCode{R}{paper/supplement/Code/code1.R}[4][8]
106 \egroup
107
108 \cmnt{Remember that the compiler is case-sensitive, so test.R is not the same as Test.R.}
109
110
111
112
113 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
114 \prob{Errors when running code.}
115 \cause\bgroup
116 In batch-mode with \verb|\runExtCode|, if the source file name does not exist, you will get a red and bold error message:\\
117 \verb|\runExtCode{julia}{test2.jl}{test2}|\\
118 \runExtCode{julia}{test2.jl}{test2}\\
119 Again, check for spelling errors in the file name, which is the most common reason for such problems.\\
120
121 \noindent Similarly, when using the \textit{server-mode} of runcode, we can use the shortcuts to R, Julia, Python, or Matlab instead of \verb|\runExtCode|. The usage is similar, but the language name is inferred from the command. For example, we can have:\\
122 \verb|\runR{paper/supplement/Code/code1.R}{testWithrunR}|
123 \runR{paper/supplement/Code/code1.R}{testWithrunR}\\
124 If the source code file doesn't exist, we get an error message as with \verb|\runExtCode|\\
125 \verb|\runJulia{test2.jl}{test2}|\\
126 \runJulia{test2.jl}{test2}\\
127 \egroup
128
129 \cause\bgroup
130 Check that the correct executable is used, since \verb|\runExtCode| requires the specific program and command line arguments. For example, using R as the executable will not work in the following example:\\
131 \verb|\runExtCode{R}{paper/supplement/Code/code1.R}{testWithR}|\\
132 %\runExtCode{R}{paper/supplement/Code/code1.R}{testWithR}
133 It will create the file generated/testWithR.tex, and in it you will see\\
134 \verb|ARGUMENT 'paper/supplement/Code/code1.R' __ignored__|\\
135 This is a fatal error -- the compiler will get stuck because it will wait for the code to finish, and the compilation process will have to be terminated by the user.
136 \noindent
137 The correct way to use \verb|\runExtCode| with R in batch-mode is\\
138 \verb|\runExtCode{Rscript --save --restore}{paper/supplement/Code/code1.R}{test}|\\
139 See the package's documentation for more working examples.\\
140 \egroup
141
142
143 \cause\bgroup
144 Check for syntax errors int the code.
145 In the \textit{server-mode} only, \verb|\inln|, \verb|\inlnR|, \verb|\inlnJulia|, \verb|\inlnPython|, and \verb|\inlnMatlab| commands can be used to execute short source code and embed the resulting output within the text. It takes 3 arguments.
146 Arg \#1 is the executable program;
147 Arg \#2 is the source code
148 Arg \#3 is the type output (if skipped or with empty value the default type is inline; vbox = verbatim in a box).
149
150 \noindent If the code contains a syntax error, the error produced by the statistical software will be embedded in the text. For example, if we use Factorial, instead of the real function (factorial) we get an error message:\\
151 \verb|The factorial of 6 is \inlnR{```cat(Factorial(6))```}.|\\
152 would produce the following:\\
153 The factorial of 6 is \inlnR{```cat(Factorial(6))```}.\\
154
155 \noindent Such errors are not due to \LaTeX~ syntax or compilation, so runcode doesn't highlight them. Automatically identifying and highlighting such errors would require case-by-case analysis of the output of specific command-line tools. For now, it's up to the user to check the code before it is embedded in the tex document, and to check the output after the compilation.
156 \egroup
157
158
159 \cmnt\bgroup
160 Recall that \verb|\runExtCode| which is used to run an external code takes 4 arguments:
161 Arg \#1 is the executable program,
162 Arg \#2 is the source file name,
163 Arg \#3 is the output file name (optional - if not given, the counter
164 codeOutput is used).
165 Arg \#4 controls when to run the code (optional - if not given, it listens
166 to \verb|\runcode|; run = force the code to run; cache or anything else =
167 use cache).\\
168 The first argument can be \textit{any} command-line executable, including user-defined program names (compiled code, aliases, etc.). Because of that, we do not perform validity check before trying to execute it.
169 For example, the following will not produce any error message:\\
170 \verb|\runExtCode{C}{test.R}{}|\\
171 but it will be possible to see an empty file in the generated folder. This will be obvious to the user once the \verb|\includeOutput| command is executed in order to embed results in the compiled pdf document.
172 \egroup
173
174
175
176 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
177
178 \prob{Output is not produced, or unexpected/incorrect results are shown.}
179 \cause\bgroup
180 This could happen due to incorrect usage of the \verb|\includeOutput| function, which is used to embed the output from executed code. It takes 2 arguments:
181 Arg \#1 is the output file name (optional - if not given, the counter codeOutput is used).
182 Arg \#2 is the optional type output with default "vbox" (vbox = verbatim in a box, tex = pure latex, or inline).
183 For example, if we run the code\\
184 \verb|\runR{paper/supplement/Code/code1.R}{testWithrunR}|
185 \runR{paper/supplement/Code/code1.R}{testWithrunR}\\
186 \noindent
187 Then to see the output, the correct usage is\\
188 \verb|\includeOutput{testWithrunR}[vbox]|\\
189 \includeOutput{testWithrunR}[vbox]
190
191 \noindent Although the physical file is named generated/testWithrunR.tex, we drop the generated/ prefix and the .tex suffix. If we do include them, the file will not be found and we'll get an error message:\\
192 \verb|\includeOutput{testWithrunR.tex}[vbox]|
193 \includeOutput{testWithrunR.tex}[vbox]
194 \egroup
195
196 \cmnt\bgroup
197 If anything other than vbox, tex, or inline is provided in the square brackets when using \verb|\includeOutput|, the output will be embedded in the document as plain text. While no error or warning is raised, this may cause problems in the compilation of the tex file and therefore should be avoided. (For example, if the output contains underscores then the compiler will report an error because it would appear that math symbols are used in text mode).\\
198 \egroup
199
200
201 \cause\bgroup
202 When using one of the \verb|inln| functions, the code does not produce any output.
203 In some cases it is perfectly fine if embedded code does not produce output, but when using \verb|\inln| this is not the case, so runcode checks if the command used within \verb|\inln| produced a zero-byte output file. If it did, runcode will show an appropriate message, like in the following example. Note that the reason no output is produced is that file.csv does not exist.\\
204 \verb|The number of columns is \inlnR{```dat <- read.csv("file.csv"); cat(ncol(dat))```}.|\\
205 This will result in:\\
206 The number of columns is \inlnR{```dat <- read.csv("file.csv"); cat(ncol(dat))```}.\\
207 \egroup
208
209 \cause\bgroup
210 When using one of the \verb|inln| functions, more than one line of output is produced by the code.
211 The \verb|\inln| commands are designed to put a single string in a line, so the code which produces the output should not include a new line. For example, the following is incorrect:\\
212 \verb|The factorial of 6 is \inlnR{```cat(factorial(6),"\\n")```}.|\\
213 The factorial of 6 is \inlnR{```cat(factorial(6),"\\n")```}.
214 \noindent
215 The correct way is:\\
216 \verb|The factorial of 6 is \inlnR{```cat(factorial(6))```}.|\\
217 which produces the following:
218 The factorial of 6 is \inlnR{```cat(factorial(6))```}.
219
220
221 \noindent
222 Similarly, if we want to print more than a one-line string, we should use the vbox option (or use \verb|\includeOutput|.) For example,\\
223 \verb|A matrix \inlnR{```matrix(1:9,ncol=3,nrow=3)```}.|\\
224 Will appear like this: A matrix \inlnR{```matrix(1:9,ncol=3,nrow=3)```}.
225 \\
226 \verb|A matrix \inlnR{```matrix(1:9,ncol=3,nrow=3)```}[vbox].|\\
227 Will look better: A matrix \inlnR{```matrix(1:9,ncol=3,nrow=3)```}[vbox].
228 \egroup
229
230
231 \cause\bgroup
232 This type of problem can also be due to invalid output in the cache files. If for some reason, the cache files contain special characters or an underscore in text-mode, then the output will be corrupted. It is recommended to clear the cache, and if server-mode is used to also restart the server. For example, suppose that the directory of the project is called proj, and we're using Python in server-mode. From the command line, run the following:
233 \begin{Verbatim}
234 cd proj
235 rm -f proj.aux generated/*.txt nohup.out proj.synctex.gz
236 python3 -c 'from talk2stat.talk2stat import client; client("./","python","QUIT")'
237 rm -f serverPIDpython.txt pythondebug.txt talk2stat.log
238 \end{Verbatim}
239 Then, recompile the project.\\
240 \egroup
241
242
243 \cause\bgroup
244 Invalid, missing, or unexpected output can be due to having another runcode running on the system, but in another directory. If this happens, you will see a message in the log file saying that the server is already running. When multiple projects attempt to connect with the statistical software through the same port, only the first invocation will succeed but the subsequent ones will be connected to the first one. This will create unexpected results for all projects using the same port. Suppose your new project is B, and the runcode server of project A is still running and using the same port. In this case you may, for example,
245 \begin{itemize}
246 \item Accidentally overwrite variables in project A, or use the wrong ones in project B if they were also defined in A;
247 \item Try to run code which is stored in project B, but you get an error message in the pdf file such as `\texttt{cannot open file 'mycode.R': No such file or directory}'. The reason for this error is that the other instance of the runcode server uses project A's base directory, and the file mycode.R is not in the right path.
248 \end{itemize}
249 To fix it, you can do one of the following:
250 \begin{enumerate}
251 \item configure each project to use another port to communicate with talk2stat (which will prevent any conflict between the projects), or,
252 \item you can stop all instances of talk2stat running on the system before compiling your current project.
253 \end{enumerate}
254 To do the latter, find all the talk2stat processes, and find the corresponding directories. For example, suppose that multiple projects use talk2stat to communicate with Julia. From the command-line, do the following:
255 \begin{Verbatim}
256 cd proj
257 ps -ef | grep talk2stat # get all the PIDs. E.g., 36797
258 # on Mac:
259 lsof -a -d cwd -p 36797
260 # on Linux:
261 pwdx 36797
262 # lsof or pwdx will give you the directory name of the other projects which run talk2stat
263 # cd to each directory, and run:
264 python3 -c 'from talk2stat.talk2stat import client; client("./","julia","QUIT")'
265 rm -f serverPIDjulia.txt juliadebug.txt talk2stat.log
266 \end{Verbatim}
267 \egroup
268
269
270 \cause\bgroup
271 If the correct output is in the files in generated directory, but it's not showing in the pdf file, it can be due to the \LaTeX~ compiler's naming of temporary files. Usually, the auto-generated file names start with the main tex file name (e.g. if the main tex file is called troubleshoot.tex then runcode will create files such as \verb|troubleshoot_inln5.tex|). However, working with Overleaf we noticed that their compiler uses another convention for temporary file names. To prevent such problems, you can add the following in the preamble of the main document:
272 \begin{Verbatim}
273 \edef\TeXjobname{\jobname} % (this line is not really essential.)
274 \edef\jobname{\detokenize{troubleshoot}}
275 \end{Verbatim}
276 \egroup
277
278
279 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
280
281
282 \prob{Working in server-mode, the server is stopped after each compilation.}
283 \cause\bgroup
284 If you are using the server-mode, be aware that some editors terminate all child processes at the end of \LaTeX compilation. For example, Emacs with Auctex behaves this way. For this case, use the nohup option, and the server will not be terminated by the parent process.\\
285 If you want the server to be stopped after each compilation (regardless of the editor you are using), use the stopserver option. While you are compiling the tex document often, you may want to keep the server side running in order to save time during initialization, and to maintain the variables and results already in memory. However, it is a good idea to use the stopserver option when you are done, just to prevent any conflicts (see above).\\
286 It's recommended to check that port numbers are unique, and when a project is not expected to be compiled for a while to enable the stopserver option.
287 \egroup
288
289
290
291
292 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
293
294
295 \prob{The process seems to hang during code execution when the \LaTeX~ document is compiled.}
296 \cause\bgroup
297 We mentioned that code being sent to the software must be syntactically valid. In addition, keep in mind that the server waits for complete commands. So, for example, if we try\\
298 \verb|The factorial of 6 is \inlnR{```cat(factorial(6)```}.|\\
299 it will cause the server to hang because it awaits the closing parenthesis. Eventually, it will time out but the code cannot be executed, so the desired output will not be produced, and an error message will replace the output, as in the following example. The timeout interval is set in the config files for R, Julia, Python, and Matlab.
300 \verb|The factorial of 6 is \inlnR{```cat(factorial(6)```}.|\\
301 The factorial of 6 is \inlnR{```cat(factorial(6)```}.\\
302 In this example the cat statement is missing a closing parenthesis.\\
303 \egroup
304
305
306
307 \end{document}
308
309