Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows for matrix commands in inline math #310

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

AJCubed
Copy link

@AJCubed AJCubed commented Jun 30, 2024

Resolves #262.

Removes the newlines from the matrix snippet expansion while in inline mode, and allows the tab/enter keys insert & or \ to even if you're inline. Under the new behavior, shift-tab will bring the cursor to the next tabstop, and shift-enter will move the cursor to the next line.

Demo:
https://github.com/artisticat1/obsidian-latex-suite/assets/64773493/130d8ad9-e9ff-4104-a589-a6af14ee8f2a

@artisticat1
Copy link
Owner

Thank you for the PR!

Comment on lines +127 to +131
// Modify matrix expansion for inline math
const regex = new RegExp("\\n\\$0\\n", "g");
replacement = replacement.replace(regex, " $0 ");

// Trim whitespace
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be needed with the current default snippets (

{trigger: "pmat", replacement: "\\begin{pmatrix}$0\\end{pmatrix}", options: "nA"},
{trigger: "bmat", replacement: "\\begin{bmatrix}$0\\end{bmatrix}", options: "nA"},
{trigger: "Bmat", replacement: "\\begin{Bmatrix}$0\\end{Bmatrix}", options: "nA"},
{trigger: "vmat", replacement: "\\begin{vmatrix}$0\\end{vmatrix}", options: "nA"},
{trigger: "Vmat", replacement: "\\begin{Vmatrix}$0\\end{Vmatrix}", options: "nA"},
{trigger: "matrix", replacement: "\\begin{matrix}$0\\end{matrix}", options: "nA"},
).

We can add the ability to remove newlines when expanding snippets inside inline math, but it should probably be separate from the trimWhitespace function

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, my environment was the issue. I'll remove this segment.

@@ -20,7 +20,7 @@ export const runMatrixShortcuts = (view: EditorView, ctx: Context, key: string,
if (!isInsideAnEnv) return false;


if (key === "Tab") {
if (key === "Tab" && !shiftKey) {
Copy link
Owner

@artisticat1 artisticat1 Jul 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has no effect ("Tab" advances to the next tabstop, not "Shift+Tab") because snippet expansion/tabout is handled before matrix shortcuts in handleKeydown.

I'd prefer to keep the original behaviour anyway ("Tab" to move to the next tabstop), unless you have an argument against it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I recall correctly I think changing settings.matrixShortcutsEnabled && ctx.mode.blockMath to strictlyInMath made it register the shortcut, but when I revise the code I'll confirm.

Re the second part: I think it makes more sense to have the inline commands identical to the block math commands, where Tab inserts an ampersand, since it'd also be the least cumbersome to type.

Right now in a block math matrix, Shift-Enter advances to the next tab stop (unless the closing $$ aren't on their own line), so we could also make Shift-Enter behave the same way inline as it does in block math. Let me know what makes the most sense here.

Copy link
Owner

@artisticat1 artisticat1 Oct 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, it makes sense to align the behaviour in inline math with that of block math.

Right now in a block math matrix, Shift-Enter advances to the next tab stop (unless the closing $$ aren't on their own line), so we could also make Shift-Enter behave the same way inline as it does in block math.

(I thought Shift-Enter moved the cursor to the end of the next line?)

So, in summary, we could do something like this. Let me know what you think.

Block math Inline math
Tab Insert & Insert &
Enter Insert \\\\\n Insert \\\\
Shift-Enter Move cursor to end of next line Move cursor to end of matrix (?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEATURE] Support matrix shortcuts in inline math
3 participants