LaTeX FAQ

LaTeX add image

  1. Upload a PNG or PDF file via CoCalc’s “Files” interface. The uploaded image should be in the same directory as the .tex file Otherwise, use relative paths like ./images/filename.png if it is in a subdirectory images.
  2. Add \usepackage{graphicx} to the preamble of your file.
  3. At the place where you want the image, insert a figure environment. Use includegraphics to include the file, with width to indicate image width, e.g. use 0.9 to take up 90% of document width.
  4. Add \centering to have your image and caption centered in the document, and use caption to add a caption.

Here’s the complete example:

\usepackage{graphicx}
...
\begin{document}
...
\begin{figure}
\centering
\includegraphics[width=0.9\textwidth]{./images/filename.png}
\caption{here is a picture}
\end{figure}
  1. There are many more options for image placement. See for example the Wikibooks LaTeX book section on Floats, Figures and Captions.

How to insert a backslash or dollar sign?

The \ character has a special meaning. It signals a LaTeX command or is used as an escape character. To enter a backslash, escape its meaning by entering it twice: \\.

A dollar sign is entered as \$, which escapes the meaning of “formula-start”.

What to do if the preview does not update

Possible reasons:

  1. Are there any errors in the “Issues” tab? LaTeX only compiles well if there are zero reported errors.
  2. Long documents could take an extended period of time to complete. In the “Preview” tab, disable the preview and only enable it once to avoid piling up too much work on the back-end.
  3. Similarly, computational-heavy “SageTeX” computations could lead to excessive compilation times. You can pre-compute results or split the document into smaller parts.

How to deal with large documents across multiple source files?

The best way is to use the subfiles package as described here. There is an extended example demonstrating how this works in the CoCalc Library. Click “Files” and open the Library. Then select Multiple source files in CoCalc in the LaTeX templates section. The same example is also available at cloud-examples/latex/multiple-files.

To do this for your existing document, let’s suppose your LaTeX project is composed of one master.tex file and several chapter-1.tex, chapter-2.tex, etc. CoCalc’s LaTeX editor only knows about the currently opened file, and using \import{} doesn’t work, because the chapter-*.tex parts are not proper documents.

The subfiles package does not only collect the partial documents into one, but also extracts the preamble of the master.tex file for each chapter-*.tex in order to create valid subdocuments.

Following the subfiles documentation, do this:

  1. \usepackage{subfiles} in master.tex

  2. \subfile{⟨subfile name ⟩} for each subfile in master.tex’s document environment (i.e. instead of \include or \import).

  3. For each chapter-*.tex subfile:

    \documentclass[⟨master.tex file-name⟩]{subfiles}
    \begin{document}
    ⟨text, graphics, etc.⟩
    \end{document}
    

After that, all *.tex files can be compiled and all other features like forward/inverse search work, too.

How to work with Knitr, SageTeX or PythonTeX?

CoCalc supports several ways to embedd code within a document. Such code is automatically processed and evaluated during generating the document and any output appears as part of the PDF output itself. In particular, you can insert small calculations and formulas (Sage, SymPy, …), Python code, R calculations, plots, data tables, etc. This is frequently used as part of reproducible research.

SageTeX

Any .tex file loading the sagetex package is automatically processed via [[SageMath]]. First, Sage code is extracted into a .sage file, then sage ... evaluates that file, and finally the LaTeX engine creates the PDF document by replacing all snippets of Sage code by their evaluated result. CoCalc handles all details for you!

To get going, you just have to insert \usepackage{sagetex} into the preamble of your document. Calculations are done like that: $\frac{2}{3.5} = \sage{n(2/17)}$, which results in SAGETEX.

See SageTeX documentation for more details and examples. There is also a SageTeX example in the CoCalc Library. Besides that, the SageMath Documentation could also be of help!

PythonTeX

PythonTeX follows the same spirit as SageTeX. Embedded Python commands and blocks of code are extracted into a .py file, Python 3 evaluates them, and at the end the LaTeX engine merges the generated output snippets into the final document and renders the PDF file. CoCalc handles all details for you!

To get going, insert \usepackage{pythontex} into the preamble of your document. Then, you can insert inline code snippets via \py{} and blocks of code inside of \begin{pyblock} and \end{pyblock}. There is also support for [SymPy] code via \sympy{} or plots via Pylab using \pylab{}.

For example, code like this:

Python code: $2+3 = \py{2+3}$

\begin{sympyblock}
x = Symbol('x')
f = x**2 * cos(x)
fi = integrate(f, x)
\end{sympyblock}

The integral of $\sympy{f}$ is $\sympy{fi.simplify()}$

produces:

_images/latex-pythontex.png

You can read more in the PythonTeX Documentation. Also note, that sometimes it is necessary to run “Build” again to properly re-process all code snippets. There is also a PythonTeX example document in the CoCalc Library.

Knitr

Knitr LaTeX documents are different from SageTeX and PythonTeX. They have their own filename extension (CoCalc supports .rnw and .Rtex) and instead of calling LaTeX commands of a package, they feature their own syntax for embedded blocks and statements. Historically, at first Sweave was added to R, but Knitr is a much more modern variant with more features (see Transition from Sweave to Knitr).

In general, the compilation works by first processing the input file via Knitr, which runs R and generates a .tex document. Then, the Latex engine processes that .tex file as usual. CoCalc handles all details for you.

To get started, create a file ending with .rnw (Rweave/Sweave syntax) or .Rtex (code is in comment blocks). Both will initialize the file with a template explaining you how to work with it. For example, a block like:

<<histogram-plot4, dev='tikz', fig.height=4, fig.width=10>>=
data <- rnorm(1000)
hist(data)
@

produces a plot of a histogram, drawn using TikZ.

_images/latex-knitr.png

Note that Forward & Inverse Search will work as well as reporting errors.

How to use PSTricks macros?

PSTricks is a set of macros for including PostScript drawings in a TeX document. The website has an extensive gallery of examples. The main thing to remember when using PSTricks is to set Engine in the CoCalc Build panel to XeLaTeX as in this small demo .tex file and resulting .pdf.

_images/latex-pstricks-demo3.png _images/latex-pstricks-demo4.png

Enable shell-escape to plot using Gnuplot

There are situations where the LaTeX document calls certain utilities to accomplish a task. One example is creating plots via Gnuplot right inside the document.

For example, a snippet of tex code could look like this:

\begin{figure}
  \begin{tikzpicture}
     \begin{axis}[ ... ]
       \addplot [...] gnuplot [raw gnuplot] {plot [-0.015:0.015] cos(380*x);};
     \end{axis}
  \end{tikzpicture}
\end{figure}

In the middle, Gnuplot runs plot [-0.015:0.015] cos(380*x); to plot a cos function.

The problem is that by default the PDF LaTeX Engine doesn’t allow to run arbitrary commands due to security concerns. You’ll see an error like that:

Package pgfplots Error: Sorry, the gnuplot-result file 'gnuplot.pgf-plot.table'
could not be found.
Maybe you need to enable the shell-escape feature? [...]

Note

You have to select the PdfLaTeX (shell-escape) engine from the selector in the build panel or modify the build command maually.

As a result, Gnuplot will be run, the error vanishes, it creates the necessary temporary files for the PGF plot, and the PDF will show the plot. You can download the example gnuplot.tex and see it in a screenshot below:

_images/latex-gnuplot-shell-escape.png

How can I draw via Asymptote?

Asymptote is a

powerful descriptive vector graphics language that provides a natural coordinate-based framework for technical drawing. Labels and equations are typeset with LaTeX, for high-quality PostScript output.

In order to tell LatexMK – which CoCalc’s LaTeX editor is using by default under the hood – to process the generated *.asy files, you need to setup your ~/.latexmkrc file in your home directory. In order to do that, open up the Files tab in your project and click on the home-icon to make sure you’re in your home directory. Then, click on Create to create a new file and enter the filename .latexmkrc. Don’t overlook that leading dot in the filename, which is used for hidden files in Linux. Then, enter these lines in the text editor and save the file:

sub asy {return system("asy \"$_[0]\"");}
add_cus_dep("asy","eps",0,"asy");
add_cus_dep("asy","pdf",0,"asy");
add_cus_dep("asy","tex",0,"asy");

These additional rules tell LatexMK to essentially run asy <basename>-*.asy on each file during the build process. In case there are problems, you can run that command-line in a Linux Terminal to see all details about any possible errors.

More information: Asymptote LaTeX Usage.

_images/latex-asymptote.png

Setup texmf in a project?

A CoCalc project is equivalent to a linux user account. Therefore, the texmf discovery mechanism works the same as on a local machine. By default, you should be able to put your own packages and styles into ~/texmf.

In case you just want to add a styles-file with common commands for the tex processor available to all your .tex files, you have to put them into the ~/texmf/tex/latex/local sub-directory. That way they’re always found by the latex processor. (see stackexchange discussion)

Otherwise, you might have to run run texhash ~/texmf in a terminal or the little “Terminal command” textbox in “Files”.

Note: the ~ stands for the HOME directory, which is the root directory you see in the “Files”-listing. You can click the home icon to jump into the home directory. texmf is a subdirectory right there.

Is there a way to turn off automatic build and PDF preview while I’m working on sub documents?

If you’re working on a main LaTeX document that has included tex files, you may want to turn off compilation of the individual subfiles. Here are some steps you can take:

  • By default, building latex documents on saving is enabled. You can disable it under AccountPreferencesEditor settings by removing the check mark for Build on save: build LaTex file whenever it is saved to disk.
  • You can also disable the build process by opening the “Build” dialog and entering echo in the line where the command is. That just does nothing when it tries to build.
  • If you like, you could also structure your LaTeX in such a way that subdocuments also build via the CTAN subfiles package. You can find an example in the CoCalc Library. In a project, click on + New, and in the middle you will find the Library. Look under LaTeX templatesMultiple source files in CoCalc.

In general, multifile LaTeX editing is on our radar, but we haven’t got to that yet: CoCalc issue #904.

An error says that the PDF cannot be built. How can I find the problem?

  • Use TimeTravel to go back to a working version. In the TimeTravel view, you can use Changes to see exactly what changed between revisions.
  • Another tip is to click the format button, since sometimes formatting properly can give you a good sense of what you might have messed up.
  • More general, you can also use revision control like [Git] to track your changes. Just create a Terminal file or frame in the latex editor and go ahead and work on the command-line as usual.
  • If you need more detailed help, make sure to open the .tex file and make a support request by clicking the Help button at the top right.

Spell check my LaTeX document

Whenever you save the LaTeX document, it will run a spell checker and underline the words that are not spelled correctly. By default, it uses the language you’ve set in your web browser.

You can change the autosave interval to be very short in account settings (under editor) if you need the spell checking to update frequently.

Seeing a list of alternative words (correct spellings) isn’t supported directly in the editor yet Issue #3461. For now, a workaround is to run LaTeX-aware aspell, e.g.

  1. Open a Linux Terminal
  2. aspell -t -c <filename.tex>

You can add words for aspell to ignore using personal dictionaries. These words won’t be underlined red. To do this create the file ~/.aspell.lang.pws, where lang is the choice of langauge. The first line of this file should be personal_ws-1.1 lang 0, where lang is the choice of language. Then add one word per line for aspell to ignore. For example, to ignore the words ‘bijection’ and ‘surjection’ in an English document, create the file ~/.aspell.en.pws with the content:

personal_ws-1.1 en 0
bijection
surjection

The changes will take place the next time aspell is run on the document. You may force this by clicking the build button.

Install packages in a project

You can install LaTeX packages in your project:

  1. Open a Linux Terminal
  2. Check by running kpsewhich -var-value TEXMFHOME where you can install packages locally. It should tell you /home/user/texm.
  3. Create the target directory based on the name of the package. E.g. if the package is called webquiz, run mkdir -p /home/user/texmf/tex/latex/webquiz.
  4. Change your current directory to this one via cd /home/user/texmf/tex/latex/webquiz.
  5. Either download the package via wget ... from CRAN and extract it via tar xf <downloaded tarball> or unzip .... Alternatively, run open . to open this path in CoCalc’s file explorer and use it to put the files there.

In any case, all files like *.sty and *.cls in that directory will be picked up when you load that package.