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


Activity
friendly commentedon Oct 12, 2024
Some related notes on this:
cov(). I'm not sure I want bordered matrices all the time, and didn't see where to turn this off. This is different, I think, than how this is controlled byrownames,colnames.\text{}\small{}or other reduced sizeAlso, the default for
digits = getOption("digits") - 2was too large, so I set that option globally,rather than in each call to
latexMatrix()philchalmers commentedon Oct 12, 2024
Hi Michael,
From my experience aligning symbols above matrices is awkward, at least if you want them centered. If you're okay with right aligning adding a few more '&' can work.
To get the symbols middle aligned you'd need to add '&' within the matrices themselves. I can take a stab at this later (right now family responsibilities are calling....).
john-d-fox commentedon Oct 12, 2024
It's been awhile since I thought about this.
Your points:
\beta_0which implies math mode. To be able to use both text and math implies a more complex design, or try using row/column names like"\\mathrm{desired text}"probably created by a new convert-to-text-mode function.print()method works very hard, if imperfectly, to try to align the column names with the contents of the matrix; changing the size of the column-names font would wreak havoc with the alignment.I think that to get everything you want in an automatic way would require a complete redesign of the how the row/column names are handled. OTOH patching the current approach should be feasible but more complicated to use.
friendly commentedon Oct 12, 2024
OK, I wasn't proposing anything in the way of re-design, just making some observations. I can try to work around-- e.g., by explicitly using
\\text{}when I re-define the colnames fromcov()The main point had to do with alignment.
john-d-fox commentedon Oct 12, 2024
I was able to quickly implement much of what you want at the expense of additional complexity. For example,
There are several new options that control default behaviour; see the code and help file for details.
I didn't implement changing the size of the row/column labels text for the reason I mentioned previously.
The changes are in the GitHub repo for the package. My testing was cursory, so the code should be exercised more.
john-d-fox commentedon Oct 12, 2024
If you want horizontal alignment to work when mixing matrices some of which have column labels and some not, I think you'll have to define empty column labels (e.g.,
""or" ") for those without. I think it would be very hard to automate that inEqn().I don't really understand why vertical alignment is so difficult. I'd be tempted simply to align on
=and let the RHSs be left-justified.john-d-fox commentedon Oct 12, 2024
I screwed around a bit with the size of the row/column labels, and the effect wasn't as extreme as I expected, so I added a
mathtext.sizeargument toprint.latexMatrix(), controlled by a corresponding option, and I changed the default formathtextfrommathrmtotextto support font-size changes. For example,So, more flexibility at the expense of a bit more complexity.
friendly commentedon Oct 12, 2024
Hi John
Your solution looks nice, but it turns out
\text{\footnotesize{}}isn't supported by MathJax. I found an online latex editor that allows different rendering engines, https://arachnoid.com/latex/vs. Codecogs, whatever that is
So, I'm not sure what to do about this commit, 30992cf
Your example works fine with just
\text{}. It is the\footnotesize` that causes the problem, so I'd say let's dispense with this.john-d-fox commentedon Oct 12, 2024
Why dispense with it rather than just not using it if the target is an html document to be processed by mathjax? BTW, the text size commands don't work in math mode, so not with, e.g.,
\mathrm{}.Another thought: There are now too many (7) options used by
print.latexMatrix(). I'd like to consolidate them in a single"print.latexMatrix"option. Any objections?john-d-fox commentedon Oct 12, 2024
I went ahead and consolidated the
"print.latexMatrix"options.Feel free to remove the
mathtext.sizeargument if you wish, though as I said I don't see the point now that it's implemented.