I'm attempting to take faster linear algebra notes within org-mode. The latex of a matrix is complex to write, so I'm trying to develop a shorthand expansion.
My current workflow is to highlight a shorthand and run M-x latex-matrixify
. Unfortunately, it seems to complain:
let: Invalid search bound (wrong side of point)
I believe because I'm adding things into the region that's selected, the bounds are not being updated to contain all of the new information.
Here's the current code:
(defun latex-matrixify ()
"turn a shorthand matrix of [2,3,4|2\\9,2,1|9] into the proper latex form"
(interactive)
(if (not (use-region-p))
(message "You have to select a region to operate on.")
(let ((pairs (list
(cons "]" "\\end{bmatrix}")
(cons "[" "\\begin{bmatrix}")
(cons "," " & ")
(cons "\\" "\\
")
(cons "|" " & | &")
)))
(dolist (p pairs)
(let ((beg (save-excursion (goto-char (region-beginning))
(line-beginning-position)))
(end (save-excursion (goto-char (region-end)) (line-end-position))))
(replace-string-in-region (car p) (cdr p) beg end))))))
It should turn something like:
[1,2,3\\4,5,6\\7,8,9]
into
\begin{bmatrix}
1 & 2 & 3\\
4 & 5 & 6\\
7 & 8 & 9\\
\end{bmatrix}
I'm not seeing a lot of details on that error.
Matrices in a hurry
.