lav_plotinfo_tikzcode: Creates code to show a diagram in tikz format

View source: R/lav_plotinfo_tikzcode.R

lav_plotinfo_tikzcodeR Documentation

Creates code to show a diagram in tikz format

Description

Creates the code to make a diagram in tikz.

Usage

lav_plotinfo_tikzcode(plotinfo,
                      outfile = "",
                      cex = 1.3,
                      sloped_labels = TRUE,
                      standalone = FALSE,
                      mlovcolors = c("lightgreen", "lightblue"),
                      lightness = 1,
                      italic = TRUE,
                      auto_subscript = TRUE
                     )

Arguments

plotinfo

A plotinfo structure as returned from lav_plotinfo_positions.

outfile

A connection or a character string, the file to store the code.

cex

Minimum distance between nodes in cm.

sloped_labels

logical, sloped labels for the edges.

standalone

logical, should code be added to make the TeX file standalone?

mlovcolors

Array of two colors for distinguishing ov nodes with the same name across the levels of a multilevel model.

lightness

A scalar factor to modify the distances between nodes.

italic

Should labels be in an italic font? If FALSE, the labels are shown in mathrm font.

auto_subscript

Logical, see lav_label_code.

Value

NULL, invisible

See Also

lav_plotinfo_positions

Examples

model <- 'alpha  =~ x1 + x2 + x3       # latent variable
          beta <~ x4 + x5 + x6         # composite
          gamma =~ x7 + x8 + x9        # latent variable
          Xi =~ x10 + x11 + x12 + x13 # latent variable
          # regressions
          Xi ~ v * alpha + t * beta + 1
          alpha ~ yy * Theta1 + tt * beta + ss * gamma
          '
test <- lav_model_plotinfo(model)
test <- lav_plotinfo_positions(test)
lav_plotinfo_tikzcode(test) # no file given, so output to R console
modelml <- '
        level: 1
        fw =~ 1*y_1 + y_2 + y_3 + y_5
        level: 2
        fb =~ 1*y_1 + y_2 + y_3 + y_4
        y_2 ~~ cv24 * y_4
  '
testml <- lav_model_plotinfo(modelml)
testml <- lav_plotinfo_positions(testml)
# in the line hereunder no file is given, so output to R console
lav_plotinfo_tikzcode(testml, cex = 1.4, sloped_labels = FALSE,
        standalone = TRUE, auto_subscript = FALSE)
## Not run: 
# example creating tex file with the above diagrams
zz <- file("testtikz.tex", open="w")
writeLines(c(
  '\documentclass{article}',
  '\usepackage{amsmath, amssymb}',
  '\usepackage{amsfonts}',
  '\usepackage[utf8]{inputenc}',
  '\usepackage[english]{babel}',
  '\usepackage{xcolor}',
  '\usepackage{color}',
  '\usepackage{tikz}',
  '\usetikzlibrary {shapes.geometric}',
  '\begin{document}'),
  zz)
lav_plotinfo_tikzcode(test, outfile = "temp.tex")
tmp <- readLines("tmp.tex")
writeLines(tmp, zz)
writeLines(" ", zz)
lav_plotinfo_tikzcode(testml, outfile = "temp.tex", cex = 1.4,
              sloped_labels = FALSE, auto_subscript = FALSE)
tmp <- readLines("tmp.tex")
writeLines(tmp, zz)
writeLines("\end{document}", zz)
close(zz)
openPDF <- function(f) {
  os <- .Platform$OS.type
  if (os=="windows")
    shell.exec(normalizePath(f))
  else {
    pdf <- getOption("pdfviewer", default='')
    if (nchar(pdf)==0)
      stop("The 'pdfviewer' option is not set. Use options(pdfviewer=...)")
    system2(pdf, args=c(f))
  }
}
tools::texi2dvi("testtikz.tex", pdf = TRUE, clean = TRUE)
openPDF("testtikz.pdf")

## End(Not run)

lavaan documentation built on Oct. 8, 2026, 5:06 p.m.