Exporting (CO)VLMC to LaTeX

news
code
R
mixvlmc
markov model
A sneak peek at mixvlmc 0.3.0: improvements to the draw functions
Published

November 25, 2026

The next release of mixvlmc, version 0.3.0, is almost ready. In addition to numerous bug fixes and a new class for discrete time series, it brings a major improvement to the draw() functions, which I present in this post.

Until it reaches the CRAN, the new version can be installed from GitHub using the standard devtools function

# install.packages("devtools")
devtools::install_github("fabrice-rossi/mixvlmc")

and then loaded with

library(mixvlmc)

The NEWS lists all the changes of this version, including the ones that break compatibility with previous versions.

Draw functions

The draw() functions are designed to give a global overview of a (CO)VLMC and are particularly adapted to small models. The versions included in mixvlmc 0.1.x and 0.2.x were heavily inspired by a similar function in VLMC.

Starting with mixvlmc 0.3.0, the functions are export functions designed both for interactive use and for preparing publication ready content. Taking inspiration from knitr::kable(), draw() now uses a format parameter that adapts the representation of the model to the target document format. mixvlmc 0.3.0 supports the "text" and "latex" formats.

Draw to text

In addition to using better default values, draw() now leverages the cli package to provide by default a UTF-8 based representation (when the output supports UTF-8), as shown below:

set.seed(42)
x <- sample(0:2, 50, replace = TRUE)
x_vlmc <- vlmc(x, 0.1)
draw(x_vlmc)
▪ (0.34, 0.42, 0.24)
├─ 1 (0.1429, 0.619, 0.2381)
└─ 2 (0.4545, 0.1818, 0.3636)
   └─ 1 (0, 0.25, 0.75)

The old “ascii art” representation is still available with the help of the draw_control() function:

draw(x_vlmc, control = draw_control(charset = "ascii"))
* (0.34, 0.42, 0.24)
+-- 1 (0.1429, 0.619, 0.2381)
'-- 2 (0.4545, 0.1818, 0.3636)
    '-- 1 (0, 0.25, 0.75)

The default can also be changed globally with the mixvlmc.charset option (see the package documentation).

Draw to LaTeX

While using UTF-8 characters improves the representation and is acceptable for interactive use and HTML documents, it is still unusable for publication ready documents, especially for scientific papers made with LaTeX. The "latex" format offers a solution based on the fantastic forest LaTeX package.

draw(x_vlmc, format = "latex")
\begin{forest}
[\multicolumn{3}{c}{\textbf{$\epsilon$}}\\\hline
{\small 0.34}&{\small 0.42}&{\small 0.24}, align=ccc
[\multicolumn{3}{c}{\textbf{1}}\\\hline
{\small 0.1429}&{\small 0.619}&{\small 0.2381}, align=ccc]
[\multicolumn{3}{c}{\textbf{2}}\\\hline
{\small 0.4545}&{\small 0.1818}&{\small 0.3636}, align=ccc
[\multicolumn{3}{c}{\textbf{1}}\\\hline
{\small 0}&{\small 0.25}&{\small 0.75}, align=ccc]]]
\end{forest}

The output is a complete forest environment that can be included in any LaTeX document whose preamble loads the forest package. The names of the states are sanitised, so that special LaTeX characters do not break the document. The same function applies to VLMC with covariates, for which draw() can also display the coefficients of the logistic models.

Integration with knitr

In mixvlmc 0.3.0, draw() does not print the model directly: it returns a representation of the model, which is printed automatically when it is not assigned to a variable. In the R console, draw(x_vlmc) displays the tree as before. The representation can also be stored and converted to a character vector, for instance to save the LaTeX code in a file:

writeLines(format(draw(x_vlmc, format = "latex")), "x_vlmc.tex")

The main benefit of this approach is the integration with knitr. In an HTML document, such as this post, the LaTeX representation is displayed as code, as shown above. But in an R Markdown or Quarto document that produces LaTeX (e.g. a PDF document), the same chunk renders the tree: the LaTeX code is included as is in the document and the forest package is loaded automatically. There is no need for a results: asis chunk option nor for a header in the document. If the preamble of the document already loads forest, possibly with options (such as \usepackage[external]{forest}), the package is not loaded a second time. This PDF document is produced by a Quarto document that contains only the code of this post.

One consequence of this change is that draw() does not display anything when its result is not printed, for instance inside a function or a loop: use print(draw(model)) in this case.