使用 AutoHotKey 简化 LaTeX 文档创作

使用 AutoHotKey 简化 LaTeX 文档创作

是否可以使用自动热键以方便 LaTeX 文档创作?以哪些方式?是否有现成的 AutoHotKey 脚本可用于此目的?

答案1

一般来说,我在输入“\”、“{”、“$”等内容时非常不准确。使用左、右、上、下键导航文本特别麻烦,而且在输入 tex 时你会不断地做这些事情。

我没有花太多时间学习如何用 AHK 编写脚本,但我确实写了一些东西来制作一些快捷方式并节省我在输入 tex 时手腕需要弯曲的量。

例如,这些是我根据自己的喜好写的。

^space:: Send {{}{}}{Left}
CapsLock:: Send {$}{$}{Left}
^9:: Send {(}{)}{Left}
^l:: Send {Right}
^j:: Send {Left}
^i:: Send {Up}
^m:: Send {Down}
^k:: Send {Down}
+^l:: Send +{Right}
+^j:: Send +{Left}
+^i:: Send +{Up}
+^m:: Send +{Down}
+^k:: Send +{Down}
^f:: Send {^}
^p:: Send \pause{Enter}{Enter}
^h:: Send \
^n:: Send \{{}\{}}{Left}{Left}
^;:: Send {End}
^Backspace:: Send {Delete}
^=:: Send {&}={&}

我来这里是因为我也想知道是否有具有 AHK 专业知识的人写过一些对编写 tex 有用的东西。

答案2

这是我随着时间的推移创建的脚本,并偶尔进行改进以供我个人使用。

特征:

  • 立即完成单括号(, [, {。您还可以使用[Shift] + [Backspace]

    在此处输入图片描述


  • 创建\left* \right*环境任何两个字符[Ctrl] + 7

    在此处输入图片描述


  • 单字母Tab自动完成为希腊字母(区分大小写)

    在此处输入图片描述


  • 使用 插入括号(, [,{来选择[Ctrl] + 8,9,0

    在此处输入图片描述


  • 根据要求机器人先生,您现在可以在项目符号之间跳转,这包含在一些自动完成命令中。这可以通过[Ctrl] + Space

    在此处输入图片描述


  • 环境Tab完成助理

    在此处输入图片描述


  • 很多自动完成功能,我不会在这里列出。请随意查看代码。它大部分都有注释。

代码(新版本):

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

#a::Suspend, Toggle ; pause script
#Hotstring EndChars `t ; [Tab] triggers Hotstrings

; Reset Hotstring for CTRL+Backspace and CTRL+A (otherwise hostrings won't trigger)
Ctrl & Backspace::
    SendInput ^`b
    Hotstring("Reset")
    Return

Ctrl & a::
    SendInput ^a
    Hotstring("Reset")
    Return

; :o:   omit ending character
; :?:   triggers hotstring even between characters
; :c:   case sensitive
; :*:   no trigger
; ^     ctrl
; +     shift
; <!    left alt



; /// 1. BRACKETS //////////////////////////////////////////////////////////////////////////////////

; instant complete (), [], {}
:o*?:(::(){Left}
:o*?:[::[]{Left}
:o*?:{::{{}{}}{Left}

:o:abs::\left|  \right| {Left 9} ; Absolut Value: \left| \right|
:o:abs2::\left|  \right|{^}2 {Left 11} ; Absolut Value: \left| \right|
^4:: Send $${Left} ; $$

; \left* \right*
; (instruction: place the cursor between the opening and the closing symbol, then hit ctrl+7)
^7::
    SendInput, {Left}\left{Right}  \right{Left 7}
    Return

; put brackets around selection
; (warning: this might not work in some applications!)
^8::
    ClipBackup := ClipboardAll
    clipboard := ""
    SendInput, ^x
    SendInput, (){Left}
    SendInput, ^v
    SendInput, {Right}
    Sleep 1000
    Clipboard := ClipBackup
    Return

^9::
    ClipBackup := ClipboardAll
    clipboard := ""
    SendInput, ^x
    SendInput, []{Left}
    SendInput, ^v
    SendInput, {Right}
    Sleep 1000
    Clipboard := ClipBackup
    Return

^0::
    ClipBackup := ClipboardAll
    clipboard := ""
    SendInput, ^x
    SendInput, {{}{}}{Left}
    SendInput, ^v
    SendInput, {Right}
    Sleep 1000
    Clipboard := ClipBackup
    Return




; /// 2. OTHER /////////////////////////////////////////////////////////////////////////////////////

; Jump to first occurance of • in the current line. (Continues search in the next line.)
^Space::
    ClipBackup := ClipboardAll
    clipboard := ""
    SendInput, {Home}+{End}
    SendInput, ^c
    Sleep 50 ; increase this number, if it doesn't work
    SearchedText := clipboard
    ; MsgBox, %SearchedText%    
    StringGetPos, pos, SearchedText, •
    if (pos >= 0)
        ; MsgBox, %Target% was found at %pos%.
        SendInput, {Home}{Right %pos%}+{Right}
    else
        SendInput, {Down}
        SendInput, {Home}+{End}
        SendInput, ^c
        Sleep 50 ; increase this number, if it doesn't work
        SearchedText := clipboard
        ; MsgBox, %SearchedText%    
        StringGetPos, pos, SearchedText, •
        if (pos >= 0)
            ; MsgBox, %Target% was found at %pos%.
            SendInput, {Home}{Right %pos%}+{Right}
        else
            SendInput, {End}
    Clipboard := ClipBackup
    Return

; Comfort
+Backspace:: Send,{BackSpace}{Delete} ; delete surrounding characters
^Enter:: Send,{End}{Enter} ; instant create new line, even from within words.

; Shift+Space: escape to the right and insert a space
+Space::
    SendInput, {Right}{Space}
    Return

; Whitespaces
:o:qd::\quad
:o:qq::\qquad
:o?: ::\{Space}{Space}
:o?:,::\,
:o:clp::\clearpage

; Environments
:o:bg::
    InputBox, OutputVar, Environment,,, 200, 120,,,,, align
    SendInput, \begin{{}%OutputVar%{}}`r`r\end{{}%OutputVar%{}}{Up}`t
    Return

:o:bga::\begin{{}align{}}`r`r\end{{}align{}}{Up}`t ; align
:o:bgas::\begin{{}align{}}\begin{{}split{}}`r`r\end{{}split{}}\end{{}align{}}{Up}`t ; align with split

:o:itmz::\begin{{}itemize{}}`r{Home}`r\end{{}itemize{}}{Up}`t\item{Space} ; align
:o:itm::\item{Space}



; /// 3. TEXT FORMATTING ///////////////////////////////////////////////////////////////////////////

; Sections
:o:sc::\section{{}•{}}{Left}+{Left} ; \section
:o:ss::\subsection{{}•{}}{Left}+{Left} ; \subsection
:o:sss::\subsubsection*{{}•{}}{Left}+{Left} ; \subsubsection*

; Text
:o:tx::\text{{}•{}}{Left}+{Left} ; \text
:o:tbf::\textbf{{}•{}}{Left}+{Left} ; \textbf
:o:tit::\textit{{}•{}}{Left}+{Left} ; \textit
:o:em::\emph{{}•{}}{Left}+{Left} ; \emph
:o:sf::\textsf{{}•{}}{Left}+{Left} ; sans serif
:o:sc::\textsc{{}•{}}{Left}+{Left} ; small caps

:o:bf::\mathbf{{}•{}}{Left}+{Left} ; \mathbf
:o:cal::\mathcal{{}•{}}{Left}+{Left} ; \mathcal
:o:scr::\mathscr{{}•{}}{Left}+{Left} ; \mathscr
:o:bb::\mathbb{{}•{}}{Left}+{Left} ; \mathbb
:o:bbm::\mathbbm{{}•{}}{Left}+{Left} ; \mathbbm



; /// 4. MATH STUFF ////////////////////////////////////////////////////////////////////////////////

; Common Operations
:o:fr::\frac{Space} ; \frac
:o:fra::\frac{{}•{}}{{}•{}}{Left 4}+{Left} ; \frac{•}{•}
:o:tfr::\tfrac{Space} ; \tfrac
:o:su::\sum{Space} ; \sum
:o:sum::\sum_{{}•{}}{^}{{}•{}}{Left 5}+{Left} ; \sum_{•}^{•}
:o:pro::\prod_{{}•{}}{^}{{}•{}}{Left 5}+{Left} ; \prod_{•}^{•}
:o:in::\int{Space} ; \int
:o:oin::\oint{Space} ; \oint
:o:int::\int_{{}•{}}{^}{{}•{}}{Left 5}+{Left} ; \int_{•}^{•}
:o:sq::\sqrt{{}•{}}{Left}+{Left} ; \sqrt{ } \sqrt{}

; alt+^ instantly yields ^{•}
<!^::
    SendInput, {^}{{}•{}}{Left}+{Left}
    Return

; Relations
:o:<=>::\Leftrightarrow ; <=>
:o:=>::\Rightarrow ; =>
:o:<=::\Leftarrow ; <=
:o?:-->::\longrightarrow ; -->
:o:->::\rightarrow ; ->
:o:!=::\overset{{}{!}{}}{{}={}}{Space} ; ! over =
:o?:=::\ =\{Space}
:o:eq::\ \equiv\{Space}
:o:~::\sim

; Other / Symbols
:o:cnc::\cancel{{}•{}}{Left}+{Left} ; \cancel{}
:o:del::\partial
:o:dell::\frac{{}\partial{}}{{}\partial •{}}{Left}+{Left}
:o:ovs::\overset{{}•{}}{{}•{}}{Left 4}+{Left}
:o:inf::\infty
:o:°::{^}\circ 
:o:cd::\cdot{Space}
:o:dg::{^}\dagger
:o:dd::\text{{}d{}}•+{Left} ; used for differentials e.g. dx



; /// 5. MATRICES //////////////////////////////////////////////////////////////////////////////////

; Matrices
:o:pm2::\begin{{}pmatrix{}}`r• & • \\`r• & • \\`r\end{{}pmatrix{}}{Left 13}{Up}`t{Left}{Up}`t+{Right}
:o:bm2::\begin{{}bmatrix{}}`r• & • \\`r• & • \\`r\end{{}bmatrix{}}{Left 13}{Up}`t{Left}{Up}`t+{Right}
:o:pm3::\begin{{}pmatrix{}}`r• & • & • \\`r• & • & • \\`r• & • & • \\`r\end{{}pmatrix{}}{Left 13}{Up}`t{Left}{Up}`t{Left}{Up}`t+{Right}
:o:bm3::\begin{{}bmatrix{}}`r• & • & • \\`r• & • & • \\`r• & • & • \\`r\end{{}bmatrix{}}{Left 13}{Up}`t{Left}{Up}`t{Left}{Up}`t+{Right}
:o:pm4::\begin{{}pmatrix{}}`r• & • & • & • \\`r• & • & • & • \\`r• & • & • & • \\`r• & • & • & • \\`r\end{{}pmatrix{}}{Left 13}{Up}`t{Left}{Up}`t{Left}{Up}`t{Left}{Up}`t+{Right}
:o:bm4::\begin{{}bmatrix{}}`r0 & 0 & 0 & 0 \\`r0 & 0 & 0 & 0 \\`r0 & 0 & 0 & 0 \\`r0 & 0 & 0 & 0 \\`r\end{{}bmatrix{}}{Left 13}{Up}`t{Left}{Up}`t{Left}{Up}`t{Left}{Up}`t+{Right}

; LGS
:o:lgs::\begin{{}matrix{}}`r• &=& • \\`r• &=& • \\`r\end{{}matrix{}}{Left 12}{Up}`t{Left}{Up}`t+{Right}

; Vectors
:o:v2::\begin{{}pmatrix{}}`r• \\`r• \\`r\end{{}pmatrix{}}{Left 13}{Up}`t{Left}{Up}`t+{Right}
:o:v3::\begin{{}pmatrix{}}`r• \\`r• \\`r• \\`r\end{{}pmatrix{}}{Left 13}{Up}`t{Left}{Up}`t{Left}{Up}`t+{Right}
:o:v4::\begin{{}pmatrix{}}`r• \\`r• \\`r• \\`r• \\`r\end{{}pmatrix{}}{Left 13}{Up}`t{Left}{Up}`t{Left}{Up}`t{Left}{Up}`t+{Right}



; /// 6. GREEK /////////////////////////////////////////////////////////////////////////////////////

:o:a::\alpha  
:o:b::\beta
:o:c::\chi
:oc:d::\delta
:oc:D::\Delta
:o:e::\epsilon
:o:ve::\varepsilon
:oc:f::\phi
:oc:F::\Phi
:o:vf::\varphi
:oc:g::\gamma
:oc:G::\Gamma
:o:h::\eta
:o:i::\iota
:o:k::\kappa
:oc:l::\lambda
:oc:L::\Lambda
:o:m::\mu
:o:n::\nu
:oc:p::\pi
:oc:P::\Pi
:oc:q::\theta
:oc:Q::\Theta
:o:vq::\vartheta
:o:r::\rho
:oc:s::\sigma
:oc:S::\Sigma
:o:t::\tau
:oc:u::\upsilon
:oc:U::\Upsilon
:o:v::\digamma
:oc:w::\omega
:oc:W::\Omega
:oc:x::\xi
:oc:X::\Xi
:oc:y::\psi
:oc:Y::\Psi
:o:z::\zeta

旧版来自我原来的回答:

; /// HEAD /////////////////////////////////////////////////////////////////////

; :o:   omit ending character
; :?:   triggers hotstring even between characters
; :c:   case sensitive
; ^     CTRL

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

#a::Suspend, Toggle ; toggle suspend script
#Hotstring EndChars `t ; [Tab] triggers Hotstrings

; Reset Hotstring for CTRL+Backspace and CTRL+A (otherwise hostrings won't trigger)
Ctrl & Backspace::
    SendInput ^`b
    Hotstring("Reset")
Return

Ctrl & a::
    SendInput ^a
    Hotstring("Reset")
Return


; /// COMFORT //////////////////////////////////////////////////////////////////

+Backspace:: Send,{BackSpace}{Delete} ; delete surrounding characters
^Enter:: Send,{End}{Enter} ; instant jump to new line, even from between characters


; /// BRACKETS /////////////////////////////////////////////////////////////////

; instant complete std brackets
:o*?:(::(){Left}
:o*?:[::[]{Left}
:o*?:{::{{}{}}{Left}

; left right brackets
^8:: Send \left(  \right) {Left 9} ; \left( \right)
^9:: Send \left[  \right] {Left 9} ; \left[ \right]
^<:: Send \left<  \right> {Left 9} ; \left< \right>
^7:: Send \left\{{}  \right\{}} {Left 10} ; \left{ \right}

^4:: Send $${Left} ; $$
:o:abs::\left|  \right| {Left 9} ; Absolut Value: \left| \right|


; /// GENERAL SHORTS ///////////////////////////////////////////////////////////

:o:baln::\begin{{}align{}}`r`r\end{{}align{}}{Up}`t ; align
:o:bgas::\begin{{}align{}}\begin{{}split{}}`r`r\end{{}split{}}\end{{}align{}}{Up}`t ; align with split
:o:sc::\section{{}•{}}{Left}+{Left} ; \section
:o:ss::\subsection{{}•{}}{Left}+{Left} ; \subsection
:o:clp::\clearpage

; Text
:o:tx::\text{{}•{}}{Left}+{Left} ; \text
:o:tbf::\textbf{{}•{}}{Left}+{Left} ; \textbf
:o:tit::\textit{{}•{}}{Left}+{Left} ; \textit
:o:em::\emph{{}•{}}{Left}+{Left} ; \emph
:o:bf::\mathbf{{}•{}}{Left}+{Left} ; \mathbf
:o:cal::\mathcal{{}•{}}{Left}+{Left} ; \mathcal
:o:scr::\mathscr{{}•{}}{Left}+{Left} ; \mathscr
:o:bb::\mathbb{{}•{}}{Left}+{Left} ; \mathbb
:o:bbm::\mathbbm{{}•{}}{Left}+{Left} ; \mathbbm

; Operations, Symbols, Relations
:o:fra::\frac{Space} ; frac
:o:sum::\sum_{{}•{}}{^}{{}•{}}{Left 5}+{Left} ; \sum
:o:pro::\prod_{{}•{}}{^}{{}•{}}{Left 5}+{Left} ; \prod
:o:int::\int_{{}•{}}{^}{{}•{}}{Left 5}+{Left} ; \int

:o:del::\partial
:o:ovs::\overset 
:o:inf::\infty
:o:°::{^}\circ 
:o:cd::\cdot
:o:dg::{^}\dagger
:o:dd::\text{{}d{}}
:o:qd::\quad
:o:qq::\qquad

:o:<=>::\Leftrightarrow
:o:=>::\Rightarrow
:o:->::\rightarrow
:o:!=::\overset{{}{!}{}}{{}={}}
:o?:=::\ =\{Space}
:o:eq::\ \equiv\{Space}
:o:~::\sim


; /// MATRICES & VECTORS ///////////////////////////////////////////////////////

; Matrices
:o:pm2::\begin{{}pmatrix{}}`r0 & 0 \\`r0 & 0 \\`r\end{{}pmatrix{}}{Left 13}{Up}`t{Left}{Up}`t
:o:bm2::\begin{{}bmatrix{}}`r0 & 0 \\`r0 & 0 \\`r\end{{}bmatrix{}}{Left 13}{Up}`t{Left}{Up}`t
:o:pm3::\begin{{}pmatrix{}}`r0 & 0 & 0 \\`r0 & 0 & 0 \\`r0 & 0 & 0 \\`r\end{{}pmatrix{}}{Left 13}{Up}`t{Left}{Up}`t{Left}{Up}`t
:o:bm3::\begin{{}bmatrix{}}`r0 & 0 & 0 \\`r0 & 0 & 0 \\`r0 & 0 & 0 \\`r\end{{}bmatrix{}}{Left 13}{Up}`t{Left}{Up}`t{Left}{Up}`t
:o:pm4::\begin{{}pmatrix{}}`r0 & 0 & 0 & 0 \\`r0 & 0 & 0 & 0 \\`r0 & 0 & 0 & 0 \\`r0 & 0 & 0 & 0 \\`r\end{{}pmatrix{}}{Left 13}{Up}`t{Left}{Up}`t{Left}{Up}`t{Left}{Up}`t
:o:bm4::\begin{{}bmatrix{}}`r0 & 0 & 0 & 0 \\`r0 & 0 & 0 & 0 \\`r0 & 0 & 0 & 0 \\`r0 & 0 & 0 & 0 \\`r\end{{}bmatrix{}}{Left 13}{Up}`t{Left}{Up}`t{Left}{Up}`t{Left}{Up}`t

; LGS
:o:lgs::\begin{{}cases{}}\;`r\begin{{}matrix{}}`rf  & = & a & b \\`rf  & = & c & d \\`r\end{{}matrix{}}`r\end{{}cases{}}{Left 11}{Up 2}`t{Left}{Up}`t

; Vectors
:o:v2::\begin{{}pmatrix{}}`r0 \\`r0 \\`r\end{{}pmatrix{}}{Left 13}{Up}`t{Left}{Up}`t
:o:v3::\begin{{}pmatrix{}}`r0 \\`r0 \\`r0 \\`r\end{{}pmatrix{}}{Left 13}{Up}`t{Left}{Up}`t{Left}{Up}`t
:o:v4::\begin{{}pmatrix{}}`r0 \\`r0 \\`r0 \\`r0 \\`r\end{{}pmatrix{}}{Left 13}{Up}`t{Left}{Up}`t{Left}{Up}`t{Left}{Up}`t


; /// GREEK ////////////////////////////////////////////////////////////////////

:o:a::\alpha  
:o:b::\beta
:o:c::\chi
:oc:d::\delta
:oc:D::\Delta
:o:e::\epsilon
:o:ve::\varepsilon
:oc:f::\phi
:oc:F::\Phi
:o:vf::\varphi
:oc:g::\gamma
:oc:G::\Gamma
:o:h::\eta
:o:i::\iota
:o:k::\kappa
:oc:l::\lambda
:oc:L::\Lambda
:o:m::\mu
:o:n::\nu
:oc:p::\pi
:oc:P::\Pi
:oc:q::\theta
:oc:Q::\Theta
:o:vq::\vartheta
:o:r::\rho
:oc:s::\sigma
:oc:S::\Sigma
:o:t::\tau
:oc:u::\upsilon
:oc:U::\Upsilon
:o:v::\digamma
:oc:w::\omega
:oc:W::\Omega
:oc:x::\xi
:oc:X::\Xi
:oc:y::\psi
:oc:Y::\Psi
:o:z::\zeta

答案3

我偶然发现了这个问题,并且很久以前我偶然找到了一个答案:

http://uweziegenhagen.de/?tag=autohotkey

那儿的那位先生。齐根哈根,博客中介绍了一些有用的脚本,以使 LaTeX 的使用更加轻松。一些示例:

!e::
InputBox, UserEnv, Environment, Please enter an environment!, , 240, 120
If ErrorLevel
    return
Else 
if( RegExMatch(UserEnv, "(.*?)(\d+)$", splitted) ) {
    Send \begin{{}%splitted1%{}}{Enter}
        Loop %splitted2% {
            Send \item {Enter}
        }
    Send \end{{}%splitted1%{}}{Up}
    count2 := splitted2 - 1 
    Loop %count2% {
        Send {Up}
    }           
} 
Else 
    Send \begin{{}%UserEnv%{}}{Enter 2}\end{{}%UserEnv%{}}{Up}
return

如果你按下Alt+e并输入例如itemize3那么脚本将插入

\begin{itemize}
\item
\item 
\item 
\end{itemize} 

我觉得这很棒!以下是更多示例:

:*:ch#::\chapter{{}{}}{LEFT}
:*:s#::\section{{}{}}{LEFT}
:*:ss#::\subsection{{}{}}{LEFT}
:*:sss#::\subsubsection{{}{}}{LEFT}

:*:b#::\begin{{}{}}{LEFT}
:*:e#::\end{{}{}}{LEFT}

:*:bf#::\textbf{{}{}}{LEFT}
:*:tt#::\texttt{{}{}}{LEFT}
:*:it#::\textit{{}{}}{LEFT}

:*:desc#::\begin{{}description{}}`r\item[]`r\item[]`r\item[]`r\end{{}description{}}{UP 3}{LEFT}
:*:enum#::\begin{{}enumerate{}}`r\item `r\item `r\item `r\end{{}enumerate{}}{UP 3}
:*:item#::\begin{{}itemize{}}`r\item `r\item `r\item `r\end{{}itemize{}}{UP 3}

也可以看看问题。以下是先生提供的更多示例。齐根哈根博客:

http://uweziegenhagen.de/?s=Autohotkey

答案4

将类似 LaTeX 的输入转换为 unicode 字符的脚本

“Ctrl+Alt+Shift+U”可打开或关闭它(查看右下角的图标可看到它处于暂停模式(图标 S)或活动模式(图标 H)。

测试:αβΓ∞¹₂ℝ∫

相关内容