Skip to main content
edited tags; edited title
Link
user31220
user31220

Replace shorthand with more expansive How do I convert a `matrix` in one form withinto a region`matrix` in another form (like `bmatrix` environment)

deleted 24 characters in body
Source Link
Drew
  • 79.1k
  • 10
  • 123
  • 257

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. Any help is appreciated.

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. Any help is appreciated.

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.

Source Link

Replace shorthand with more expansive form within a region

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. Any help is appreciated.