Skip to content

Align symbolic and numeric matrices #66

Description

@friendly

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:

image

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:

image

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 =.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions