LatexModePlugin

This LaTeX Mode Plugin allows you to include LaTeX mark up commands within a Foswiki page. It uses external programs (specifically latex, dvipng or dvips-and-convert, or mimetex) to generate png or gif images from the mark up. These images are then included in the rendered Foswiki page. The first time a particular image is generated, there may be a significant lag in page rendering as the images are generated on the server. Once rendered, the image is saved as an attached file for the page, so subsequent viewings will not require re-renders. When you remove a math expression from a page, its image is deleted.

This plugin expands the functionality provided by the Foswiki:Extensions.MathModePlugin.

Syntax Rules

The plugin interprets a number of delimiters to declare LaTeX markup strings. For example, if the LatexModePlugin is successfully installed, the string \int_{-\infty}^\infty e^{-\alpha x^2} dx = \sqrt{\frac{\pi}{\alpha}} will render as an image $\int_{-\infty}^\infty e^{ -\alpha x^2 } dx = \sqrt{ \frac{\pi}{\alpha} }$, when enclosed within the defined delimiters.

Standard Syntax

This plugin has two standard modes:
  • in-line, declared by %$ $%. Similar to LaTeX's inline math mode, the math markup is rendered on the same line as other text in the string.
  • own-line, declared by %\[ \]% or %MATHMODE{ ... }%. These equations will be rendered with center justification on their own line.

For the majority of users, these commands should be sufficient

Example

This is an example of the \LaTeX rendering possibilities using the LatexModePlugin.

The singular value decomposition of a matrix $A$ is defined as
  \begin{displaymath} A = U \Sigma V^H \end{displaymath} (1)

where $U$ and $V$ are both matrices with orthonormal columns, $\{\cdot\}^H$ indicates a complex-conjugate transpose, and $\Sigma$ is a diagonal matrix with singular values
\[ \sigma_1 > \sigma_2 > \cdots > \sigma_n \geq 0 \]
along the main diagonal. Eq. (1) is just one of the many matrix decompositions that exists for matrix $A$.

After the plugin has been succesfully installed and configured, this example should render like this:
expl-v1.4.png

Extended Syntax

For those that are well familiar with LaTeX, a multi-line syntax allowing more complicated markup commands can be declared using
%BEGINLATEX%
  \begin{<environment>}
	 _latex markup_ 
  \end{<environment>}
%ENDLATEX%
Typically, the declared <environment> will be displaymath, although there is no limitation.

Additional options can be included to modify the rendered result. These include
option possible values default description
inline 0 or 1 0 controls inline vs ownline rendering
label alpha-numeric --- produces a linkable equation number for ownline markup
density positive integer 116 (set below) controls rendered font size
scale positive number 1.0 (set below) sets post-rendered image scaling
gamma positive number 0.6 (set below) controls rendered font darkness
bgcolor --- 'white' sets background color (details below)
color --- 'black' sets foreground font color (details below)
attachment alpha-numeric --- allows one to couple the latex command with an attached file. Useful for latex graphics commands.
engine {"", "dvipng", "ps", "pdf", "mimetex"} "" dynamically switch the rendering engine between the installation default (""), dvipng, dvips+convert, pdflatex+convert, or mimetex for LaTeX packages than need it, e.g. tikz. (details below)

For example, to declare an equation to be numbered by Foswiki (not in the LaTeX image) with a larger font size and in red, use the following syntax:

%BEGINLATEX{label="eq1" density="175" color="red"}%

latex markup

%ENDLATEX%

HTML references to LaTeX equations with a defined <label> can be generated using %REFLATEX{<label>}%.

Rendering options

Both DENSITY and SCALE alter the rendered image size and quality. For example, if one doubles the DENSITY and halves the SCALE, the rendered image resolution will improve but keep the same image size on the rendered page. (Note: DENSITY * SCALE is the same in both cases)
density = 116, scale = 2.0 :
 \[ \mathcal{A } \]
density = 232, scale = 1.0 :
 \[\mathcal{A}\]

For regular browser viewing, the SCALE parameter sould be set to 1.0. However, one can use these parameters to improve print quality when printing a topic. To do this, increase the DENSITY setting (a value of 300 will give roughly 300dpi) and then set the SCALE setting below 1.0.

Font Color

As of v1.3, one can now directly control the foreground font color in the rendered mathematics. This is achieved through use of the color.sty package in the intermediate latex file.

Latex is able to render colors defined in 3 color spaces: gray, rgb, and cmyk. A limited number of colors are predefined in Latex. These include:
color color space color space value
black gray 0
white gray 1
red rgb 1,0,0
green rgb 0,1,0
blue rgb 0,0,1
cyan cmyk 1,0,0,0
magenta cmyk 0,1,0,0
yellow cmyk 0,0,1,0

For convenience, the following Foswiki colors are pre-defined in the LatexModePlugin
   \definecolor{Red}{rgb}{1,0,0}
   \definecolor{Blue}{rgb}{0,0,1}
   \definecolor{Yellow}{rgb}{1,1,0}
   \definecolor{Orange}{rgb}{1,0.4,0}
   \definecolor{Pink}{rgb}{1,0,1}
   \definecolor{Purple}{rgb}{0.5,0,0.5}
   \definecolor{Teal}{rgb}{0,0.5,0.5}
   \definecolor{Navy}{rgb}{0,0,0.5}
   \definecolor{Aqua}{rgb}{0,1,1}
   \definecolor{Lime}{rgb}{0,1,0}
   \definecolor{Green}{rgb}{0,0.5,0}
   \definecolor{Olive}{rgb}{0.5,0.5,0}
   \definecolor{Maroon}{rgb}{0.5,0,0}
   \definecolor{Brown}{rgb}{0.6,0.4,0.2}
   \definecolor{Black}{gray}{0}
   \definecolor{Gray}{gray}{0.5}
   \definecolor{Silver}{gray}{0.75}
   \definecolor{White}{gray}{1}

To use additional colors, they need to be defined in the Latex preamble, as described in the next section.

Including images in LaTeX markup

v3.0 introduced the ability to include attachments in the latex markup processing. This is most useful for graphics, e.g.

%BEGINLATEX{attachment="fig1.eps"}%

  \includegraphics{fig1.eps}

%ENDLATEX%

It is common practice in LaTeX, however, to not specify the filename extension. This is implemented in the Plugin as well, so one could type:
%BEGINLATEX{attachment="fig1"}%

  \includegraphics{fig1}

%ENDLATEX%

and the plugin will search for an attachment with extension '.eps', '.eps.gz', '.pdf', '.png', or '.jpg'. The first extension match will be used, and the rendering engine that can recognize the attachment will also be automatically determined. So for the example above, if a file 'fig1.eps' is attached to the topic, it will be used as the attachment and dvips+convert will be automatically chosen as the rendering engine.

Switching the rendering on the fly

v3.3 introduced the ability to switch rendering dynamically between dvipng, dvips+convert, and pdflatex+convert. Some latex packages are not supported by the preferred method dvipng, for example TikZ and PGF. Rather than force all rendering to use a slower background rendering engine, this switch allows one to use dvipng rendering as the default, but fall back to 'ps' or 'pdf' intermediate files in certain cases.

If you type:
%BEGINLATEX{ engine="ps"}%

\fbox{ 
  \begin{tikzpicture}[auto,bend right] 
    \node (a) at (0:1) {$0^\circ$}; 
    \node (b) at (120:1) {$120^\circ$}; 
    \node (c) at (240:1) {$240^\circ$}; 
    \draw (a) to node {1} node [swap] {1'} (b) 
          (b) to node {2} node [swap] {2'} (c) 
          (c) to node {3} node [swap] {3'} (a); 
  \end{tikzpicture} 
} 

%ENDLATEX%
    It will render as:

TiKZ example

This enables a wide variety of LaTeX packages to be used within Foswiki. For example: simple image manipulations using the graphicx package. Note that the package must be declared in the LaTeX preamble.
If you type:
%BEGINLATEX{attachment="fig2.png" engine="pdf"}%

  \includegraphics[angle=30,scale=0.25]{fig2.png}

%ENDLATEX%
    It will render as:

rotation example

Tables, Figures, and cross-references

To round out the functionality available in standard LaTeX, the automatic generation of Figure and Table reference links is also available. These are declared using
  • %BEGINFIGURE{label="fig:label" caption="this is a figure" span="twocolumn"}% ... %ENDFIGURE%, and
  • %BEGINTABLE{label="tbl:label" caption="this is a table" span="twocolumn"}% ... %ENDTABLE%.

These commands will create an HTML table with a numbered caption either above (TABLE) or below (FIGURE) the included text. Cross-references to these declared environments can be accessed through %REFLATEX{<label>}%. To keep the counters/cross-references seperate for each of the three types of references, use eq: or eqn:, fig:, and tbl: as the first characters in any declared label.

The span option is used only by Foswiki:Extensions.GenPDFLatex, giving the ability to designate the width of a table or figure in two-column styles. (e.g. for Figures, it expands to \begin{figure*} ... \end{figure*}.) The default span is one-column.

Sections can be numbered and labeled, for easy cross-referencing. To label a section, add a %SECLABEL{_label_}% tag after the Foswiki section command. E.g.,
   ---++ %SECLABEL{sec:intro}% Introduction
Cross-references to the label can be generated using %REFLATEX{sec:intro}%.

To add automatic numbering to the sections, set the following parameter to a non-zero number. Sections up to this depth will be numbered.
  • Set MAXSECDEPTH = 3
The default setting is '0', which disables the numbering and section labels.

Defining the LaTeX preamble

In LaTeX, the preamble is used to customize the latex processing, allowing one to add custom styles and declare new commands.

In Foswiki, the preamble can be set as either a web or topic preference setting
   * #Set PREAMBLE = \usepackage{color} \definecolor{Aqua}{rgb}{0,1,1}
or as a multi-line declaration, using the tags:
   %BEGINLATEXPREAMBLE% ... %ENDLATEXPREAMBLE%

One critical difference between the two exists. With the exception of the color declarations above, the Foswiki preference setting will override the default settings, and is intended to provide site administrators a central point to set preamble settings globally. In contrast, the tag declaration will add to the preamble defined by either the default settings or the preference setting, allowing Foswiki users to amend the preamble.

Common Symbols

Since the LatexModePlugin is not installed on Foswiki.org the above external html reference is given so that you can see what the symbols are. For those who do use Latex in your Foswiki install, you can copy the tables formatted for Foswiki from the following topics (the symbols won't actually display without the plugin).

What to type to get a variety of symbols using Latex. Due to page loading constraints, the symbols tables are split up into 5 different topics.

Plugin Settings

Plugin settings are stored as preferences settings. To reference these plugin settings write %<plugin>_<setting>%, i.e. %LATEXMODEPLUGIN_SHORTDESCRIPTION%

  • One line description, as shown in the TextFormattingRules topic:
    • Set SHORTDESCRIPTION = Enables LaTeX markup (mathematics and more) in Foswiki topics

  • Debug plugin: (See output in data/debug.txt)
    • Set DEBUG = 0

  • Set the dots-per-inch default density level for the rendered images (116 is suggested for 11pt browser fonts.)
    • Set DENSITY = 116

  • Set the gamma correction for the rendered images. This controls how dark the rendered text appears
    • Set GAMMA = 0.6

  • Set the scaling for the image size. Typically, set to '1'.
    • Set SCALE = 1.0

  • Uncomment the following to define a site-wide custom color, DarkBlue.
    • #Set PREAMBLE = \usepackage{color} \definecolor{DarkBlue}{rgb}{0,0.1,0.43}

  • In addition, the following parameters are available at the topic level, EQN, FIG, TBL, to override the counter reset. (i.e. if    * Set EQN = 3 is declared in a topic, the first labeled equation will be given the number 4).

Note: It is recommended to declare these settings in the WikiPreferences or WebPreferences topics, so that they aren't lost when the plugin is upgraded. In this case, the macro declarations should be preceded by LATEXMODEPLUGIN_. E.g. to the set the font density default for a specific web, WEB, use:
  • Set LATEXMODEPLUGIN_DENSITY = 200
in the topic WEB.WebPreferences.

Plugin Installation Instructions

First, confirm that the external software needed by the plugin is installed on the Foswiki server. This includes:

Rendering can be performed by (latex and dvipng) or (latex and dvips and convert) or (pdflatex and convert) or (mimetex). The first three options allow one to include almost any LaTeX markup in a Foswiki topic, whereas mimetex has very limited functionality. Among the first three options, dvipng is the fastest by a significant margin. The tweakinline processing (v2.5 and above) to align the baseline of LaTeX expressions with HTML text uses convert.

mimetex can be used in server environments where a full LaTeX installation is impractical (e.g. on shared-server installations). mimetex provides rendering of equations independently of external font files. This provides a very lightweight and fast mechanism to show equations. However, the number of colors is limited to 4 (black, red, blue, green) and only the mathmode and picture LaTeX environments are supported. Also, the preamble, density, and gamma settings are ignored.

Second,
  • Download the LatexModePlugin.zip file from the Plugin web
    warning Note: versions 4.0 and above are compatible only with Foswiki 1.x.x. warning

  • Unzip ZIP file in your Foswiki installation directory. Content:
    File: Description:
    lib/Foswiki/Plugins/LatexModePlugin.pm Plugin Perl module
    lib/Foswiki/Plugins/LatexModePlugin/Init.pm The initialization module
    lib/Foswiki/Plugins/LatexModePlugin/Render.pm The rendering module
    lib/Foswiki/Plugins/LatexModePlugin/CrossRef.pm The cross-referencing module
    pub/System/LatexModePlugin/expl-v1.4.png example image of rendered latex  
    pub/System/LatexModePlugin/tikz-expl.png image for on-the-fly rendering example
    pub/System/LatexModePlugin/rot-expl.png image for attachment rendering example
    data/System/LatexModePlugin.txt Plugin topic and documentation  
    data/System/LatexSymbols.txt Comprehensive list of available LaTeX symbols (1 of 5)
    data/System/LatexSymbols2.txt Comprehensive list of available LaTeX symbols (2 of 5)
    data/System/LatexSymbols3.txt Comprehensive list of available LaTeX symbols (3 of 5)
    data/System/LatexSymbols4.txt Comprehensive list of available LaTeX symbols (4 of 5)
    data/System/LatexSymbols5.txt Comprehensive list of available LaTeX symbols (5 of 5)
    data/System/LatexIntro.txt A basic description of LaTeX markup conventions

Finally, customize the installation specific configuration settings.
  • Set the local disk paths for the rendering methods employed. This can be done by copying the needed lines from the following list to lib/LocalSite.cfg
    • $Foswiki::cfg{Plugins}{LatexModePlugin}{latex} = '/usr/bin/latex';
    • $Foswiki::cfg{Plugins}{LatexModePlugin}{pdflatex} = '/usr/bin/pdflatex';
    • $Foswiki::cfg{Plugins}{LatexModePlugin}{dvips} = '/usr/bin/dvips';
    • $Foswiki::cfg{Plugins}{LatexModePlugin}{dvipng} = '/usr/bin/dvipng';
    • $Foswiki::cfg{Plugins}{LatexModePlugin}{convert} = '/usr/X11R6/bin/convert';
    • $Foswiki::cfg{Plugins}{LatexModePlugin}{mimetex} = '/usr/bin/mimetex';
  • Modify the following if needed.
    • $Foswiki::cfg{Plugins}{LatexModePlugin}{donotrenderlist}
      declare a comma-separated list of LaTeX commands that will not be rendered.
      Default = 'input,include,catcode'.
    • $Foswiki::cfg{Plugins}{LatexModePlugin}{tweakinline}
      Turned off by default, if this is set to 1 the plugin will attempt to align the baseline of the rendered in-line math with the baseline of the HTML text.
    • $Foswiki::cfg{Plugins}{LatexModePlugin}{bypassattach}
      Turned off by default, setting this boolean setting to 1 will force file creation to use direct file stores and bypass the saveAttach mechanism in Foswiki. Saves a bit of processing overhead.
    • $Foswiki::cfg{Plugins}{LatexModePlugin}{engine}
      This sets the default rendering engine
      Default: 'dvipng'
    • $Foswiki::cfg{Plugins}{LatexModePlugin}{imagetype}
      This sets the image format for the generated files. Valid types are png and gif.
      Default: 'png'

Security

Aside from providing beautiful rendering of mathematics, LaTeX is fundamentally a programming language. Before installation of this plugin, one should consider the implications of exposing access to a programming language on a web server. Foswiki's use of access control can mitigate some of the risk, by limiting access to trusted users. Complementary to this approach, one can prevent certain commands from being rendered using the {donotrenderlist} configuration setting.

To start, before installing the Plugin, one should modify the texmf.cnf file on the server to the following variables:
    shell_escape = f
    openout_any = p
    openin_any = p  % note this won't work on Windows

Next, one should declare the donotrenderlist. At a minimum, the LaTeX commands of input, include, and catcode should be in the list. On publicly editable wiki's, the commands newcommand and def should be added as well. newenvironment, newfont, newtheorem, and newsavebox should be considered as well.

Finally, one should set a limit on the length of time allowed for latex to finish its processing. This can be done in Apache via RLimit settings.

More Details

Version control is not specifically used for the image files. Because the images are generated from the raw text, the topic history includes all the versions of the markup for the expressions, and can be re-rendered when you view a different version.

This plugin is an enhanced version of the Foswiki:Extensions.MathModePlugin maintained by Foswiki:Main.MichaelDaum. There are a number of significant differences:
  • equations, tables, and figures can be numbered, with automatic cross-links available
  • if errors occur during the latex markup processing, they are reported during the editing preview screen, but not during the standard view. The motivation for this is that, in general, authors care about the error but readers do not.
  • as of version 2.5, one can force the recreation of all latex images in a topic. To do this, add the following text (similar to raw text rendering) to the end of the URL: ?latex=rerender. E.g. changing http://localhost/foswiki/bin/view/System/LatexModePlugin to http://localhost/foswiki/bin/view/System/LatexModePlugin?latex=rerender will force all of the images to be redrawn.
  • as of version 2.6, one can bypass the file attachment code in Foswiki 4.x.x. Foswiki 4.x.x and above is designed to accommodate non-filesystem-storage (i.e. databases) for attachments. This plugin has supported this interface since v2.0. However, using the Foswiki4 attachment storage mechanism adds a significant delay in plugin response time. So, a new configuration setting has been introduced to allow admins to bypass the Foswiki4 interface and use direct file-system stores if they so choose. To activate, set
       $Foswiki::cfg{Plugins}{LatexModePlugin}{bypassattach} = 1;
    
    in LocalSite.cfg

Additional Resources (external)

Plugin Info

Plugin Author: Foswiki:Main.ScottHoge
Plugin Version: 27 Aug 2009 (v 4.0, SVN:17761 (2014-06-10))
Change History: warning v4.0 and above requires Foswiki 1.x.x and above warning
10 Jun 2014 (v 4.2) removed all use of depreciated function 'defined'
15 Jun 2010 (v 4.1) added option for multiple attachments in LaTeX rendering
27 Aug 2009 (v 4.0) Ported to Foswiki and fixed a few bugs.
Change History: warning v3.x requires TWiki 4.x.x and above warning
27 Oct 2008 (v 3.74) replaced plugin init with lazy-loading, to fix installer bug
24 Jun 2008 (v 3.73) fewer bugs: TOC interaction corrected, cant-see-mimetex-images-in-win32 bug fixed
02 Dec 2007 (v 3.72) more bug fixes: sync doc to code, fixed trimline v2 for ps engine
15 Nov 2007 (v 3.71) minor bug fixes: DEFAULTENGINE, doc changes, improved the 'stale time' in delete-image-check
-- Apr 2007 (v 3.62) added -halt-on-error for better error handling. Fixed debug variable in Init.pm
12 Mar 2007 (v 3.6) New option, imagetype, to set png or gif rendering from LocalSite.cfg
2 Feb 2007 (unreleased) Modified the section labeling to be more compatible with genpdflatex, minor cleanup of html markup, new inline processing available (set tweakinline equal to '2' to enable)
30 Dec 2006 (v 3.51) fixed call to attachmentExists in Render.pm, corrected version number
30 Dec 2006 (v 3.5) modified how rendering of math from included topics is handled. Fixed mimetex inline processing
27 Dec 2006 (v 3.4) added mimetex to the rendering engine list
27 Nov 2006 (v 3.35) improved dynamic rendering engine to auto-select on graphics files
17 Nov 2006 (v 3.3) added dynamic switching of rendering engine, more Parse bug fixes
06 Oct 2006 (v 3.2) more bugs fixed: bgcolor option was missing from v3.0; verbatim mode fixed in Parse
04 Oct 2006 (v 3.1) fixed two bugs: populating files to check under Dakar, and itemize env parsing in Parse
30 Sep 2006 (v 3.0) Significant rewrite of module, including: section numbering/cross-links and mod_perl compatibility.
25 Sep 2006 (v 2.62) fixed handleFloat to allow TWiki markup tags in captions.
8 Aug 2006 (v 2.61) fixed INCLUDE-not-rendering bug introduced in v2.6. Aded bgcolor option. Split symbol list into 5 topics.
5 Aug 2006 (v 2.6) added security description and expanded default donotrenderlist. Reworked plugin init to reduce overhead when not in use. Added bypassattach option. Sandbox now used in place of system calls.
19 May 2006 (v 2.51) bug fix: rerender hook block of mailnotify corrected
14 Mar 2006 (v 2.5) added rerender hook, fixed '> in alt field' bug.
21 Feb 2006 (v 2.4) introduced donotrenderlist to patch a critical security hole. Bug fixes include: disabling WikiWord link rendering in alt fields of img tags; improved in-line rendering alignment available;
1 Feb 2006 (v 2.3) minor bug fixes: $pathSep changes, now uses &TWiki::Func::extractParameters(), improved efficiency and inline rendering
11 Nov 2005 (v 2.2) more mods for Foswiki:Extensions.GenPDFLatexAddOn: protect newlines, moved float handler, moved float label checker
15 Oct 2005 (v 2.1) minor modifications for Foswiki:Extensions.GenPDFLatexAddOn support
unreleased (v 2.0) Major rewrite for Dakar
30 Sep 2005 (v 1.41) relaxed the scrubing a little bit... previous version caused problems with REFLATEX
29 Sep 2005 (v 1.4) more robust scrubing of convert input parameters. errors on save now reported.
5 Sep 2005 (v 1.3) added image scale parameter, color rendering, and preamble hooks
22 Aug 2005 (v 1.2) Forked from the Foswiki:Extensions.MathModePlugin by TWiki:Main.GraemeLufkin
Foswiki Dependency: $Foswiki::Plugins::VERSION 2.0
CPAN Dependencies: CPAN:Digest::MD5, CPAN:File::Basename, CPAN:Image::Info
Other Dependencies: A working installation of latex. A working installation of convert or dvipng.
Perl Version: 5.8.0
License: GPL (GNU General Public License)
Plugin Home: http://foswiki.org/Extensions/LatexModePlugin
Feedback: http://foswiki.org/Extensions/LatexModePluginDev

This plugin was created and tested on the 02 Sep 2004 version of TWiki, using ImageMagick v5.5.7, EPS GhostScript v7.05, and tetex v2.02. It has been reported to work (Thanks Jos!) using ImageMagick 6.2.3, tetex 3.0 and ghostscript 8.51 as well.

Related Topics: DefaultPreferences, SitePreferences, Plugins, Foswiki:Extensions.MathModePlugin
Topic revision: r1 - 07 Feb 2018, UnknownUser
This site is powered by FoswikiCopyright © by the contributing authors. All material on this site is the property of the contributing authors.
Ideas, requests, problems regarding Foswiki? Send feedback