Blog 1

Random Talk on Random Thoughts

Generate $\rm \LaTeX$ Code for Octave Matrices

| Comments |

Background

Problem

Recently, I encountered a linear algebra problem on Mathematics Stack Exchange.

For any given matrix $A$ in Octave,

A = [1 2 2; 2 3 4; 4 4 2]
A =

   1   2   2
   2   3   4
   4   4   2

how can one generate its $\rm \LaTeX$ code

$\rm \LaTeX$ code for matrix $A$
1
A = \begin{bmatrix} 1&2&2\\ 2&3&4\\ 4&4&2 \end{bmatrix}

so that one gets

\[ A = \begin{bmatrix} 1&2&2\\ 2&3&4\\ 4&4&2 \end{bmatrix}? \]

Solution

I searched “octave to latex matrices” and I found this answer quite useful. I issued the first command to see the result.

strrep(strrep(mat2str(A),",","&"),";","\\\\\n")(2:end-1)
ans = 1 2 2\\
2 3 4\\
4 4 2

I suspected that it didn’t work. I posted it as a comment and verified that I was right. To fix this, I extracted the function mat2str(A) in the middle of this command to see the results. From its result (ans = [1 2 2;2 3 4;4 4 2]), I realised that strrep standed for “string replace”. Then, I changed the double-quoted comma in the second argument of function strrep to a double-quoted whitespace character, and got the anticipated result.

strcat("\\begin{bmatrix}\n",strrep(strrep(mat2str(A)," ","&"), ...
";","\\\\\n")(2:end-1),"\n\\end{bmatrix}\n")
ans = \begin{bmatrix}
1&2&2\\
2&3&4\\
4&4&2
\end{bmatrix}

Inadequacies

I don’t know how to do this for matrices with fractions. I think I can work it out by searching and testing in several hours, but I don’t have the time to do so.

Lessons learnt

Math

I revised some definitions in linear algebra.

  • A matrix $U$ is unitary iff $UU^\star = U^\star U = I$.
  • A matrix $N$ is normal iff $NN^\star = N^\star N$.

GNU Octave

  • The function strrep(str,"foo","bar") replaces all instances of foo in str with bar. It can be used in a nested manner for multiple replacements.
  • The function strcat(str1,str2,...) concatenates the strings inside.

Grep

When I was writing this article, I wanted to search for “octave” (with the square brackets “[]”) with grep inside Vim, but I got over 1270 results. I tried adding single/double quotes and escaping the square brackets with a backslash, but I faied again. Finally, I googled “grep escape character” for a solution. Since then, I know that I should add the -F flag to grep to fix the string.

Comments