James-Yu / LaTeX-Workshop

Boost LaTeX typesetting efficiency with preview, compile, autocomplete, colorize, and more.
MIT License
10.65k stars 530 forks source link

[Feature Request] Font styling commands #806

Closed tecosaur closed 6 years ago

tecosaur commented 6 years ago

Idea

This isn't something I feel particularly strongly about, however, it is definitely something that I would appreciate.

It would be nice if LaTeX workshop had the ability to apply \textbf{...}, \emp{...}, to selected text - ideally with different rules for text/math mode.

Why?

Because while it's not a huge amount of effort adding tags around selected text manually,

  1. This is done enough that the key savings would (for me) be worth it
  2. Using an arrow key is surprisingly disruptive to my 'typing flow' (and I feel that I'm probably not the only one)
  3. While many users may not make use of this, those that do will value it, and it should not negatively impact the experience of those that don't

Three Ideas for Implementation

Ordered from least versitile to most versitile.

Simple Shortcuts

Since Ctrl+B is already bound, perhaps Alt+B/I/… would be a better alternative.

Two-part Shortcuts

Or one of those two key hotkeys (i.e. 'Ctrl+K Ctrl+C') - which would then better allow for all the text variants latex has, i.e. After an initial key combo, the "Single Key" from the table below would trigger the appropriate wrapping/unwrapping

Command Palate

Shortcut for a new command such as "Modify Text Style", which when selected would give an array of options, that can be searched and selected, with hardcoded selections based on the "Single Key" and "Two Key" columns of the table below. I'd imagine this would feel similar to that unicode character searching extension to use.

The Aforementioned Table

Single Key Two Key Text Mode Math Mode Description
n no \textnormal{...} \mathnormal{...} Normal
e em \emph{...} Emphasised
r rm \textrm{...} \mathrm{...} Roman Font
b bf \textbf{...} \mathbf{...} Bold
bb \mathbb{...} Black board bold
i it \textit{...} \mathit{...} Italic
sl \textsl{...} Slanted
t tt \texttt{...} Teletype
c sc \textsc{...} Small Caps
c ca \mathcal{...} Calligraphic
s sf \textsf{...} Sans Serif
u up \uppercase{...} Uppercase
l lo \lowercase{...} Lowercase

How much of an improvement is this really?

Apart from \emph and \textbf I know I don't use many of those other ones frequently,and when applying \emph to a section of text it's not too hard, but because it's done frequently enough I would definitely appreciate the effort-saving. Example:

Example Current With Method 1+3
Emphasising Text [6 key] [2 key]
Select Text → { → left arrow → \em → enter Select Text → alte
Making R → ℝ [8 key] [4 key]
Select R{\mathb → enter Select R → Shortcut → bb → enter

Not life-changing, but appreciable.

Final Note(s)

Astrantia commented 6 years ago

https://github.com/James-Yu/LaTeX-Workshop/commit/213002b3ba2b1b1464f2b60d67a78096ddbb53ff Wasn't this commit supposed to address this? The name of the commit suggests yes

I like this request a lot

tecosaur commented 6 years ago

Seems like I've probably spent a while writing out a request for a feature that already exists 😛. Now time to try out the stuff in that commit.

Astrantia commented 6 years ago

@tecosaur Thing is I don't know how you try it because there are no keybindings for the commands provided

tecosaur commented 6 years ago

I've tried:

However that just overwrites the selection with \emph{}

James-Yu commented 6 years ago

I've tried the above steps exactly as shown and can get the correct result. Anyone may provide a screencast on the problematic behavior?

Astrantia commented 6 years ago

I'm getting correct results too with the steps u described @tecosaur However in light of this Issue, I think it will be nice to implement CTRL+B/Bold CTRL+I/Italics etc...

Also I don't think binding CTRL+B is an issue because in vscode you can add the when clause which in our case will be to trigger the command only when a text is selected.

tecosaur commented 6 years ago

Re: Current snippet not working

Here's a demo of it not working. ezgif.com-optimize.gif After you said it worked for you though, I tried it on my other computer and it worked ¯\_(ツ)_/¯.

Re: More shortcuts

I didn't know the when clause could be applied to text selection, if it can I think adding stuff like Ctrl+B would be a worthwhile addition!

Re: Current snippets

I have some ideas for improving them / adding more (math mode etc.). So I may create a pull request doing that.

Astrantia commented 6 years ago

@tecosaur What's that theme if you don't mind me asking? 😁

tecosaur commented 6 years ago

@Astrantia It may be off topic, but here's how I have my editor (visually) set up.

I use Monokai Pro and just ignore the 'Pay for me' prompts, then I've also installed the font Fira Code and enabled ligatures. Highlights come from this extension, and then I also use Bracket Pair Colorizer. End result, the editor looks like this: editor.png code_theme_example.png

tecosaur commented 6 years ago

@James-Yu Do you know if it's possible to have a when clause for text/math mode? Also looking to see if there's a keybindings file, and if not what one would have to do to get one working.

tecosaur commented 6 years ago

Update:

Have added more formatting things: see here Also written and tested shortcuts (see below).

    {
        "key": "ctrl+b",
        "when": "editorHasSelection && editorLangId == 'latex'",
        "command": "editor.action.insertSnippet",
        "args": {
            "name": "textbf"
        }
    },
    {
        "key": "ctrl+i",
        "when": "editorHasSelection && editorLangId == 'latex'",
        "command": "editor.action.insertSnippet",
        "args": {
            "name": "textit"
        }
    },
    {
        "key": "ctrl+u",
        "when": "editorHasSelection && editorLangId == 'latex'",
        "command": "editor.action.insertSnippet",
        "args": {
            "name": "underline"
        }
    },
    {
        "key": "ctrl+e",
        "when": "editorHasSelection && editorLangId == 'latex'",
        "command": "editor.action.insertSnippet",
        "args": {
            "name": "textem"
        }
    },
    {
        "key": "ctrl+t",
        "when": "editorHasSelection && editorLangId == 'latex'",
        "command": "editor.action.insertSnippet",
        "args": {
            "name": "texttt"
        }
    }
jlelong commented 6 years ago

Actually, this feature already existed in the extension. It was broken, but I have just fixed it.

  1. Select some text
  2. Press \
  3. A popup shows up with a list of commands. Type a few letters of the command and there you are.

Note: if you use the vim-mode, it only works in insert mode, not in visual one.

tecosaur commented 6 years ago

@jlelong While that is good, to me it doesn't quite seem to fully address what's being discussed here. While it may not be a life-changing addition, what I was thinking of was something more like this: ezgif.com-video-to-gif.gif So far this uses some of a commit I made to my fork of this repo (this) and the shortcuts pasted earlier.

tecosaur commented 6 years ago

Re: Keyboard Shortcuts

I recently stumbled across https://latextools.readthedocs.io/en/latest/keybindings/ and ftp://ftp.gnu.org/gnu/auctex/11.89-extra/tex-ref.pdf - some bits may be good food for thought. I'd definitely be interested in implementing some select keybindings.

Swoy commented 6 years ago

Isn't this a dupe of #434?

Astrantia commented 6 years ago

@Swoy Why close #434 though? I understand it an be configured manually, but it will be easier if all the configurations are shipped by the extension. For example, the most popular markdown editor ships with CTRL+B mapping to *{selection}*, etc...

tecosaur commented 6 years ago

I'm considering renaming and rewriting this issue, or starting a new issue - since (at least in my mind) this seems to have evolved beyond just font emphasis (really only since I discovered the documents linked in my earlier reply - and as a result, my LaTeX-workshop-keyboard-shortcut horizons have been expanded).

Re: Keyboard Shortcuts

I recently stumbled across https://latextools.readthedocs.io/en/latest/keybindings/ and ftp://ftp.gnu.org/gnu/auctex/11.89-extra/tex-ref.pdf - some bits may be good food for thought. I'd definitely be interested in implementing some select keybindings.

tecosaur commented 6 years ago

@James-Yu question that will be repeated in the new issue: Do you know if there's a way to use a when clause to differentiate between text and math mode? (asking now because you seem to be online, and I'm still writing that issue up)

James-Yu commented 6 years ago

@tecosaur I have no idea on such a when condition unfortunately 😢

tecosaur commented 6 years ago

How is it defined to use separate syntax highlighting etc. compared to normal in the extension? And what sort of terms are used when registering it with VS Code? I'm basically looking for good keywords to use for online searching.

James-Yu commented 6 years ago

@jlelong Expert in syntax highlighting. Lemme ping him for you. 🤣

tecosaur commented 6 years ago

Also, does anybody know of any extensions that have keyboard shortcuts that only work when you're working in certain parts of the file?

jlelong commented 6 years ago

I do not think it is possible to restrict a keybinding to parts of a file. See here for the list of when clause. Getting the scope at the current character already seems to be a problem: vscode#15287 and vscode#18485.

tecosaur commented 6 years ago

Oh well, from what you linked it looks like a scope filter isn't likely to be around any time soon :( Thanks for investigating it.

jlelong commented 6 years ago

I guess we can close this issue and continue chatting on #809 concerning how to add keybindings to the existing snippets.