LaTeX code can be added to the description of topics, questions and answers to represent mathematical formulas, tables or graphs. For example:
[tex]
\begin{displaymath}
\mathop{\mathrm{corr}}(X,Y)=
\frac{\displaystyle
\sum_{i=1}^n(x_i-\overline x)
(y_i-\overline y)}
{\displaystyle\biggl[
\sum_{i=1}^n(x_i-\overline x)^2
\sum_{i=1}^n(y_i-\overline y)^2
\biggr]^{1/2}}
\end{displaymath}
[/tex]
The TCExam LaTeX renderer converts the code to a PNG image that is cached in the cache/ folder
and reused for later calls with the same code.
[tex] in text written by examinees (free-text answers and test comments) is shown as text and is
not compiled.
Restrictions
pdflatex runs in the cache/ folder with openin_any=p openout_any=p and -no-shell-escape:
hidden files, parent directories and absolute paths outside TEXMFOUTPUT cannot be opened, and
shell commands cannot be run. A formula is rejected before compilation when it contains commands
that read or write files, define macros, change category codes or control the output, including
\input, \include, \def, \catcode, \read, \write, \special, \pdf...
primitives, \scantokens, \immediate and \shipout. A rendered image larger than
K_LATEX_MAX_WIDTH / K_LATEX_MAX_HEIGHT is deleted.
Requirements
LaTeX rendering requires the following additional software to be installed on the server:
- LaTeX — https://www.latex-project.org/ or https://tug.org/texlive/ (on Windows, MiKTeX is a good option);
- ImageMagick — https://imagemagick.org/.
Configuration
Edit shared/config/tce_latex.php (shipped as shared/config.default/tce_latex.php) to match
your system. The relevant constants include:
| Constant | Purpose |
|---|---|
K_LATEX_PATH_CONVERT | path to the ImageMagick convert (or magick) binary |
K_LATEX_PDFLATEX | path to the pdflatex (LaTeX) binary |
K_LATEX_FONT_SIZE | font size in points (default 10) |
K_LATEX_CLASS | LaTeX document class (default article) |
K_LATEX_FORMULA_DENSITY | output image density / DPI |
K_LATEX_MAX_WIDTH / K_LATEX_MAX_HEIGHT | maximum rendered image dimensions |
K_LATEX_IMG_FORMAT | output image format (default png) |
To find executable paths on Linux, use which pdflatex and which convert. On Windows, use
dir /x to find the short (DOS) path to the executables. The renderer implementation lives in
shared/code/tce_latexrender.php.
Notes
Debugging. To see raw errors while diagnosing problems, temporarily comment out the error-handler registration in
shared/code/tce_functions_errmsg.php://$old_error_handler = set_error_handler('F_error_handler', K_ERROR_TYPES);Font sizes. The default document class (
article) only supports 10, 11 and 12 pt font sizes. For smaller or larger fonts, install theextsizespackage from CTAN, refresh the TeX database (texhash, or MiKTeX Options → Refresh Now on Windows), then setK_LATEX_FONT_SIZEinshared/config/tce_latex.phpaccordingly.Multi-line environments. You can build equation arrays and other
\begin{…}blocks by prefacing them with two newlines.Display style. Render formulae in display style with
\displaystyle.Per-formula sizing. Even with a default size set, you can resize a single formula with
\mbox, e.g.\mbox{\huge\sqrt{2}}or\mbox{\footnotesize\sqrt{2}}.