-
Notifications
You must be signed in to change notification settings - Fork 17
Description
Starting to use latexMatrix() and friends in my book. I ran into a little problem with aligning two equations, one symbolic and the other numeric. My test file is dev/eigen-ex.R
Goal: show the eigen decomposition of a cov matrix, with a numeric example, ie,
S = V Lambda V^T
data(workers, package = "matlib")
head(workers)
vars <- c("Experience", "Income")
# covariance matrix & mean
mu <- colMeans(workers[, vars]) |> print()
S <- cov(workers[, vars]) |> print()
# eigenvalues and eigenvectors
S.eig <- eigen(S)
Lambda <- S.eig$values |> print()
V <- S.eig$vectors |> print()
where,
S <- cov(workers[, vars]) |> print()
Experience Income
Experience 135.8333 151.9444
Income 151.9444 233.6111
I tried using \phantom{} to give the symbolic part look like "S = V Lambda V^T"
(fiddling with the size of the phantom)
# align V Lambda V' using \phantom{}
options(digits = 4)
rownames(S) <- colnames(S) <- c("Exp", "Inc")
spacer <- "\\phantom{00000000000000}"
Eqn("\\mathbf{S} & = \\mathbf{V}", spacer,
"\\mathbf{\\Lambda}", spacer,
"\\mathbf{V}^\\top", Eqn_newline(),
latexMatrix(S), "& =",
latexMatrix(V), " ", diag(Lambda), " ", latexMatrix(V, transpose=TRUE),
align = TRUE)
which gives:
Note too bad, but very fiddly. Is there a better LaTeX way? I tried other alignment (&) tabs, but there's
something I don't understand.
# What's wrong here?
spacer <- " & "
Eqn("\\mathbf{S} & = \\mathbf{V}", spacer,
"\\mathbf{\\Lambda}", spacer,
"\\mathbf{V}^\\top", Eqn_newline(),
latexMatrix(S), "& =",
latexMatrix(V), " ", diag(Lambda), " ", latexMatrix(V, transpose=TRUE),
align = TRUE)
Gives:
I think this is more of a LaTeX issue, than a problem in the package.
I looked at Eqn_hspace(), but couldn't figure out how to make it work in this context, because the function and docs related to alignment around =.

