reddit / emacs

Want to create a function that writes out latex matrices

MrPezevenk · 2022-05-29 20:34
Hey, I'm very new to emacs lisp programming etc, and I wanted to write a function that automatically writes out a latex matrix for me. The idea is that I give it a number of rows, a number of columns, and it automatically writes out the latex for me. I guess it's kinda like a code snippet. I don't really have to know how to write this function step by step, I can figure it out if I get going, but I can't get going because I don't know how I make a function write something on my buffer... Can anyone help me with that?
SpikeyWallaby · 2022-05-29 23:24
Look at https://www.gnu.org/software/emacs/manual/html_node/elisp/Insertion.html for inserting text into the buffer. `insert` is the basic primitive to use.
karthink · 2022-05-30 05:30
You can write a [yasnippet](http://joaotavora.github.io/yasnippet/snippet-development.html#org73e1209) for this that prompts you for the number of rows and columns. Alternatively, you can use `orgtbl-mode` to convert an Org table (which are very intuitive and fast to create) to a matrix. [Here is a demo](https://karthinks.com/img/latex-table-demo-3.mp4) and an [explanation](https://karthinks.com/software/latex-input-for-impatient-scholars/#table-matrix-or-array-entry).
MrPezevenk · 2022-05-30 05:35
Hmm ok I will check it out!
MrPezevenk · 2022-05-30 05:35
Ok thanks I will look into it.
sunnyata · 2022-05-30 13:50
Yasnippet is the way to go if you just want to get this working and move on but it sounds like you want to learn elisp too, so OP's advice is good.
MrPezevenk · 2022-05-30 13:55
Yes you got it right, I want to do both.
dm_g · 2022-05-30 22:12
Not an answer to your question, but how I solve the problem you have: Whenever I have to create a latex table, I usually create it in org-mode first. I then export the table with org-table-export format latex. I then edit the latex for typesetting finetunning.
MrPezevenk · 2022-05-30 22:23
My basic issue with that is that I want the tables to be in latex previews INSIDE org mode and I can't be bothered to copy and paste them all the time...
dm_g · 2022-05-31 00:53
>ramming etc, and I wanted to write a function that automatically writes out a latex matrix for me. The idea is that I give it a number of rows, a number of columns, and it automatically writes out the latex for me. I guess it's kinda like a code snippet. > >I don't really have to know how to write this function step by step, I can figure it out if I get going, but I can't get going because I don't know how I make a function write something on my buffer... Can anyone help me with that? Is this what you are looking for? (require 'seq) (defun doltable (spec rows) "insert latex table" (interactive "sEnter column specification: nEnter number of rows:") (insert (concat "\\begin{table}[" spec "]\n" (cl-reduce 'concat (make-list rows (concat (cl-reduce 'concat (make-list (- (length spec) 1) " & ")) "\\\\\n"))) "\\end{table}\n"))) if called it will ask you for the spec of the table: eg: llr and the number of rows. Note that it simply counts the number of chars in the spec to compute the number of columns. The code needs to be updated to deal with table separators. For example: calling doltable and then typing llr and 5 yields: \begin{table}[llr] & & \\ & & \\ & & \\ & & \\ & & \\ \end{table}
MrPezevenk · 2022-05-31 05:49
Yes (except matrices, not tables, what I said before was dumb, but it's the same more or less)! Although you spoiled it for me hahahah