Sakana 小组件集成

Contents

Optimize Your TexStudio Editing Experience

For Chinese-speaking users, there is probably nothing more frustrating than wanting to type $$ but ending up with ¥¥ due to not switching input methods in time.

Although there are now a series of $\LaTeX$ online compilation websites like overleaf, texpage, slager, they still struggle to provide convenience for this issue. Online rendering is becoming increasingly restrictive. If you want to try compiling larger, more complex, or longer documents without paying, local software is always the best choice (as long as there is no need for collaborative editing), which also means greater freedom.

Macro Commands

For Chinese-speaking $\LaTeX$ users, there is probably nothing more frustrating than wanting to type $$ but ending up with ¥¥ due to not switching input methods in time.

What kept me using Obsidian is its extremely convenient writing plugins:

  • Quick commands: commander, editing toolbar
  • Image conversion: image converter
  • Writing enhancement: easy typing

Especially the writing enhancement plugin easy typing, which supports custom completions, automatic spacing between Chinese and English, seamless Chinese-English character switching—for example, typing ¥¥ twice will automatically become $$.

So the question is, how to make TexStudio achieve the same functionality?

The answer is custom macros. TexStudio supports custom macro definitions. For detailed syntax, refer to the official documentation Personal macros.

Normal mode macros can be understood as text transformation and completion. I write ¥¥¥¥ in the trigger (note: intentionally leaving a space at the end). %| represents the cursor position after completion.

Personal macros can also have custom shortcuts. For example, in the image below, I set Ctrl+M as the shortcut for inserting $$. When I type ¥*2 (space), it will also automatically trigger the transformation.

/img/texstudio.zh-cn.md-1760090933518.webp
Triggers are format detection that automatically activate when regular expressions match

Personal macros also support script sequences. If you know how to use them, you can create various tricks (like chained transformations). Therefore, when writing macros, pay attention to the order—place macros with stricter regular expression checks above. For example, ¥¥¥¥ to $$$$ should be placed before ¥¥ to $$.

Environment mode feels quite redundant and can be completely replaced by normal mode.

Script mode is more flexible and supports the JavaScript language.

I wrote one that can transform content in the format -content1-content2-content2 (tab key) into code:

1
2
3
\begin{content1}[content2]\label{content2}

\end{content1}

I also further set up a mapping: if content1 is “证明”, it transforms to “proof”; if it’s “概念”, it transforms to “definition”.

The code is as follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// ===== Chinese-English environment mapping =====
var envMap = {
    "定理": "theorem",
    "引理": "lemma",
    "命题": "proposition",
	"性质": "proposition",
	"练习": "exercise",
	"思考题": "exercise",
	"思考": "exercise",
	"问题": "exercise",
    "推论": "corollary",
    "定义": "definition",
	"概念": "definition",
    "例子": "example",
    "备注": "remark",
	"注意": "remark",
    "笔记": "note",
	"注释": "note",
    "案例": "case",
    "证明": "proof",
	"证": "proof",
    "练习": "exercise"
};

// ===== Get matched content =====
var env = triggerMatches[1];   // environment
var title = triggerMatches[2]; // title
var label = triggerMatches[3]; // label

// ===== Convert Chinese environment to LaTeX English environment =====
if (envMap[env]) {
    env = envMap[env];
}

// ===== Construct LaTeX code =====
var txt = "\\begin{" + env + "}";
if (title) txt += "[" + title + "]";
if (label) txt += "\\label{" + label + "}";
txt += "\n\n\\end{" + env + "}\n";

// ===== Insert text =====
cursor.insertText(txt);

// ===== Adjust cursor position inside the environment =====
cursor.movePosition(2, cursorEnums.Up);
cursor.movePosition(1, cursorEnums.StartOfLine);

The trigger is set as:

1
2
3
\-(.+)\-(.+)\-(.+)\t
*\t is the tab key
*(.+) : Parentheses represent an environment, representing the content between separators -. Where .+ represents one or more characters. If at least two characters are required, use .+. instead.

The logic isn’t difficult either. The three parentheses in the trigger’s regular expression correspond to env, title, and label in the code. Fill them into the code template, then move the cursor. (The only problem is that TexStudio’s regular expression recognition has poor compatibility with Chinese characters.)

/img/texstudio.zh-cn.md-1760491203042.webp
As shown in the image

Configuring Language Assistant in TexStudio

TexStudio page.

/img/LaTeX入门.zh-cn-1758093039527.webp
As shown in the image

First, download Java (please find tutorials yourself).

Then download the free grammar checking assistant—https://languagetool.org/download/

Download the version corresponding to your computer. I’m using Windows system with the latest TexStudio, so I chose LanguageTool-6.6.zip.

After downloading, extract the corresponding LanguageTool zip file. Open cmd in the corresponding folder page (just type cmd in the address bar to open it from here).

Then run java -jar languagetool.jar

After opening, go to text checking - options to enter the page, set language, server port.

/img/LaTeX入门.zh-cn-1758092612033.webp
As shown in the image

After setting up, go to the LanguageTool section at the bottom of TexStudio settings.

/img/LaTeX入门.zh-cn-1758092768418.webp
As shown in the image

After restarting TexStudio, you can check the language tool in help:

The wavy lines correspond to grammar errors.

/img/LaTeX入门.zh-cn-1758092931198.webp
As shown in the image

Right-click on the error location for correction suggestions:

/img/LaTeX入门.zh-cn-1758092975675.webp
As shown in the image

Some Keyboard Shortcuts

  • Ctrl + Left Mouse Click: Quickly navigate (jump to source code or corresponding PDF position).
  • Ctrl + Shift + I: Quickly add \item (for list environments itemize/enumerate).
  • Ctrl + T: Quickly add/remove comments (for current line or selected multiple lines).
  • Ctrl + Shift + M: Quickly add inline code, for example, add $ x $ at cursor position.
  • Ctrl + Shift + N: Add equation environment (e.g., \begin{equation} ... \end{equation}).
  • Alt + Shift + M: Add displayed code (usually math environment, e.g., $$...$$ or \[ ... \]).

Other Tips

Make good use of the symbol collection function in the left column.

/img/texstudio.zh-cn.md-1760092010830.webp
As shown in the image

Right-click in the code editing page to jump to PDF rendering position. Press Ctrl key and click on PDF position to jump to code editing position.

TexStudio supports inline preview. Alt+P to start, but it may conflict with other template rendering settings. For example, when I use the Elegant series template, I cannot view the preview.

/img/texstudio.zh-cn.md-1760238587723.webp
TexStudio supports inline preview. Alt+P to start

References