lsp-latex icon indicating copy to clipboard operation
lsp-latex copied to clipboard

Emacs lsp-mode client for LaTeX, on texlab.

[[https://github.com/ROCKTAKEY/lsp-latex][https://img.shields.io/github/tag/ROCKTAKEY/lsp-latex.svg?style=flat-square]] [[file:LICENSE][https://img.shields.io/github/license/ROCKTAKEY/lsp-latex.svg?style=flat-square]] [[https://github.com/ROCKTAKEY/lsp-latex/actions][https://img.shields.io/github/workflow/status/ROCKTAKEY/lsp-latex/CI/master.svg?style=flat-square]] [[https://melpa.org/#/lsp-latex][file:https://melpa.org/packages/lsp-latex-badge.svg]]

  • lsp-mode client for LaTeX.
  • How to Use?
    • First, you have to install ~texlab~. Please install this [[https://github.com/latex-lsp/texlab/releases][here]].
    • Next, you should make ~lsp-mode~ available. See [[https://github.com/emacs-lsp/lsp-mode][lsp-mode]].
    • Now, you can use Language Server Protocol (LSP) on (la)tex-mode or yatex-mode just to evaluate this:

#+BEGIN_SRC emacs-lisp -n (add-to-list 'load-path "/path/to/lsp-latex") (require 'lsp-latex) ;; "texlab" must be located at a directory contained in `exec-path'. ;; If you want to put "texlab" somewhere else, ;; you can specify the path to "texlab" as follows: ;; (setq lsp-latex-texlab-executable "/path/to/texlab")

(with-eval-after-load "tex-mode" (add-hook 'tex-mode-hook 'lsp) (add-hook 'latex-mode-hook 'lsp))

;; For YaTeX (with-eval-after-load "yatex" (add-hook 'yatex-mode-hook 'lsp))

;; For bibtex (with-eval-after-load "bibtex" (add-hook 'bibtex-mode-hook 'lsp)) #+END_SRC

  • Variables ** ~lsp-latex-texlab-executable~ Where texlab server located. ** ~lsp-latex-texlab-executable-argument-list~ Argument list passed to texlab server. ** Others, provided by texlab server These variables are connected to texlab configuration variables. See also texlab [[https://github.com/latex-lsp/texlab/blob/master/docs/options.md][documentation]]. | Custom variable in Emacs | Configuration provided by texlab | |------------------------------------------+-------------------------------------| | lsp-latex-root-directory | texlab.rootDirectory | | lsp-latex-build-executable | texlab.build.executable | | lsp-latex-build-args | texlab.build.args | | lsp-latex-build-aux-directory | texlab.build.outputDirectory | | lsp-latex-build-is-continuous | texlab.build.isContinuous | | lsp-latex-forward-search-after | texlab.build.forwardSearchAfter | | lsp-latex-forward-search-executable | texlab.forwardSearch.executable | | lsp-latex-forward-search-args | texlab.forwardSearch.args | | lsp-latex-chktex-on-edit | texlab.chktex.onEdit | | lsp-latex-chktex-on-open-and-save | texlab.chktex.onOpenAndSave | | lsp-latex-diagnostics-delay | texlab.diagnosticsDelay | | lsp-latex-bibtex-formatter-line-length | texlab.formatterLineLength | | lsp-latex-bibtex-formatter | texlab.bibtexFormatter | | lsp-latex-latex-formatter | texlab.latexFormatter | | lsp-latex-latexindent-local | texlab.latexindent.local | | lsp-latex-latexindent-modify-line-breaks | texlab.latexindent.modifyLineBreaks |

  • Build ** ~lsp-latex-build~ Build .tex files with texlab. It use latexmk by default, so add .latexmkrc if you want to customize latex commands or options. You can change build command and option to other such as make, by changing ~lsp-latex-build-executable~ and ~lsp-latex-build-args~.

    This command build asynchronously by default, while it build synchronously with prefix argument(C-u).

  • Forward/inverse search Forward search and inverse search are available. See also [[https://github.com/latex-lsp/texlab/blob/master/docs/previewing.md][document of texlab]].

** Forward search You can move from Emacs to current position on pdf viewer by the function ~lsp-latex-forward-search~. To use, you should set ~lsp-latex-forward-search-executable~ and ~lsp-latex-forward-search-args~ according to your pdf viewer.

You can see [[https://github.com/latex-lsp/texlab/blob/master/docs/previewing.md][document of texlab]], but you should replace some VSCode words with Emacs words. ~latex.forwardSearch.executable~ should be replaced with ~lsp-latex-forward-search-executable~, and ~latex.forwardSearch.args~ with ~lsp-latex-forward-search-args~. You should setq each variable instead of writing like json, and vector in json is replaced to list in Emacs Lisp. So the json: #+BEGIN_SRC json :tangle yes { "texlab.forwardSearch.executable": "FavoriteViewer", "texlab.forwardSearch.args": [ "%p", "%f", "%l" ] } #+END_SRC should be replaced with the Emacs Lisp code: #+begin_src emacs-lisp :tangle yes (setq lsp-latex-forward-search-executable "FavoriteViewer") (setq lsp-latex-forward-search-args '("%p" "%f" "%l")) #+end_src

In ~lsp-latex-forward-search-args~, the string "%f" is replaced with "The path of the current TeX file", "%p" with "The path of the current PDF file", "%l" with "The current line number", by texlab (see [[https://github.com/latex-lsp/texlab/blob/master/docs/options.md#texlabforwardsearchargs][here]]).

For example of SumatraPDF, write in init.el: #+begin_src emacs-lisp :tangle yes (setq lsp-latex-forward-search-executable "C:/Users/{User}/AppData/Local/SumatraPDF/SumatraPDF.exe") (setq lsp-latex-forward-search-args '("-reuse-instance" "%p" "-forward-search" "%f" "%l")) #+end_src while VSCode config with json (see [[https://github.com/latex-lsp/texlab/blob/master/docs/previewing.md#forward-search][document of texlab]]) is: #+BEGIN_SRC json :tangle yes { "texlab.forwardSearch.executable": "C:/Users/{User}/AppData/Local/SumatraPDF/SumatraPDF.exe", "texlab.forwardSearch.args": [ "-reuse-instance", "%p", "-forward-search", "%f", "%l" ] } #+END_SRC

Then, you can jump to the current position on pdf viewer by command ~lsp-latex-forward-search~.

** Inverse search You can go to the current position on Emacs from pdf viewer. Whatever pdf viewer you use, you should start emacs server by writing in init.el: #+begin_src emacs-lisp :tangle yes (server-start) #+end_src Then, you can jump to line {{LINE-NUMBER}} in file named {{FILENAME}} with the command: #+BEGIN_SRC shell -n emacsclient +{{LINE-NUMBER}} {{FILENAME}} #+END_SRC {{LINE-NUMBER}} and {{FILENAME}} should be replaced with line number and filename you want to jump to. Each pdf viewer can provide some syntax to replace.

For example of SmatraPDF (see [[https://github.com/latex-lsp/texlab/blob/master/docs/previewing.md#inverse-search][document of texlab]]), "Add the following line to your SumatraPDF settings file (Menu -> Settings -> Advanced Options):" #+BEGIN_SRC ini -n InverseSearchCmdLine = C:\path\to\emacsclient.exe +%l %f #+END_SRC Then, "You can execute the search by pressing Alt+DoubleClick in the PDF document".

** Examples These examples are according to [[https://github.com/latex-lsp/texlab/blob/master/docs/previewing.md#inverse-search][document of texlab]]. Especially, quoted or double-quoted sentences are citation from [[https://github.com/latex-lsp/texlab/blob/master/docs/previewing.md#inverse-search][document of texlab]]. *** SumatraPDF #+BEGIN_QUOTE We highly recommend SumatraPDF on Windows because Adobe Reader locks the opened PDF file and will therefore prevent further builds. #+END_QUOTE **** Forward search Write to init.el: #+begin_src emacs-lisp :tangle yes (setq lsp-latex-forward-search-executable "C:/Users/{User}/AppData/Local/SumatraPDF/SumatraPDF.exe") (setq lsp-latex-forward-search-args '("-reuse-instance" "%p" "-forward-search" "%f" "%l")) #+end_src **** Inverse Search #+BEGIN_QUOTE Add the following line to your [[https://www.sumatrapdfreader.org/][SumatraPDF]] settings file (Menu -> Settings -> Advanced Options): #+END_QUOTE #+BEGIN_SRC ini -n InverseSearchCmdLine = C:\path\to\emacsclient.exe +%l "%f" #+END_SRC #+BEGIN_QUOTE You can execute the search by pressing =Alt+DoubleClick= in the PDF document. #+END_QUOTE *** Evince #+BEGIN_QUOTE The SyncTeX feature of [[https://wiki.gnome.org/Apps/Evince][Evince]] requires communication via D-Bus. In order to use it from the command line, install the [[https://github.com/latex-lsp/evince-synctex][evince-synctex]] script. #+END_QUOTE **** Forward search Write to init.el: #+begin_src emacs-lisp :tangle yes (setq lsp-latex-forward-search-executable "evince-synctex") (setq lsp-latex-forward-search-args '("-f" "%l" "%p" ""emacsclient +%l %f"")) #+end_src **** Inverse search #+BEGIN_QUOTE The inverse search feature is already configured if you use =evince-synctex=. You can execute the search by pressing =Ctrl+Click= in the PDF document. #+END_QUOTE *** Okular **** Forward search Write to init.el: #+begin_src emacs-lisp :tangle yes (setq lsp-latex-forward-search-executable "okular") (setq lsp-latex-forward-search-args '("--unique" "file:%p#src:%l%f")) #+end_src **** Inverse search #+BEGIN_QUOTE Change the editor of Okular (Settings -> Configure Okular... -> Editor) to "Custom Text Editor" and set the following command: #+END_QUOTE #+begin_src shell :tangle yes emacsclient +%l "%f" #+end_src You can execute the search by pressing =Shift+Click= in the PDF document. *** Zathura **** Forward search Write to init.el: #+begin_src emacs-lisp :tangle yes (setq lsp-latex-forward-search-executable "zathura") (setq lsp-latex-forward-search-args '("--synctex-forward" "%l:1:%f" "%p")) #+end_src **** Inverse search #+BEGIN_QUOTE Add the following lines to your =~/.config/zathura/zathurarc= file: #+END_QUOTE #+BEGIN_SRC shell -n set synctex true set synctex-editor-command "emacsclient +%{line} %{input}" #+END_SRC #+BEGIN_QUOTE You can execute the search by pressing =Alt+Click= in the PDF document. #+END_QUOTE *** qpdfview **** Forward search Write to init.el: #+begin_src emacs-lisp :tangle yes (setq lsp-latex-forward-search-executable "qpdfview") (setq lsp-latex-forward-search-args '("--unique" "%p#src:%f:%l:1")) #+end_src **** Inverse search #+BEGIN_QUOTE Change the source editor setting (Edit -> Settings... -> Behavior -> Source editor) to: #+END_QUOTE #+BEGIN_SRC shell -n emacsclient +%2 "%1" #+END_SRC #+BEGIN_QUOTE and select a mouse button modifier (Edit -> Settings... -> Behavior -> Modifiers -> Mouse button modifiers -> Open in Source Editor)of choice. You can execute the search by pressing Modifier+Click in the PDF document. #+END_QUOTE *** Skim #+BEGIN_QUOTE We recommend [[https://skim-app.sourceforge.io/][Skim]] on macOS since it is the only native viewer that supports SyncTeX. Additionally, enable the "Reload automatically" setting in the Skim preferences (Skim -> Preferences -> Sync -> Check for file changes). #+END_QUOTE **** Forward search Write to init.el: #+begin_src emacs-lisp :tangle yes (setq lsp-latex-forward-search-executable "/Applications/Skim.app/Contents/SharedSupport/displayline") (setq lsp-latex-forward-search-args '("%l" "%p" "%f")) #+end_src "If you want Skim to stay in the background after executing the forward search, you can add the =-g= option to" =lsp-latex-forward-search-args=. **** Inverse search Select Emacs preset "in the Skim preferences (Skim -> Preferences -> Sync -> PDF-TeX Sync support). You can execute the search by pressing =Shift+⌘+Click= in the PDF document." *** ~pdf-tools~ integration If you want to use forward search with ~pdf-tools~, follow the setting: #+begin_src emacs-lisp :tangle yes ;; Start Emacs server (server-start) ;; Turn on SyncTeX on the build. ;; If you use `lsp-latex-build', it is on by default. ;; If not (for example, YaTeX or LaTeX-mode building system), ;; put to init.el like this: (setq tex-command "platex --synctex=1")

  ;; Setting for pdf-tools
  (setq lsp-latex-forward-search-executable "emacsclient")
  (setq lsp-latex-forward-search-args
        '("--eval"
          "(lsp-latex-forward-search-with-pdf-tools \"%f\" \"%p\" \"%l\")"))
#+end_src
Inverse research is not provided by texlab,
so please use ~pdf-sync-backward-search-mouse~.
  • Note In this package, you can use even texlab v0.4.2 or older, written with Java, though it is not recommended. If you want to use them, you can write like: #+BEGIN_SRC emacs-lisp -n ;; Path to Java executable. If it is added to environmental PATH, ;; you don't have to write this. (setq lsp-latex-java-executable "/path/to/java")

    ;; "texlab.jar" must be located at a directory contained in exec-path' ;; "texlab" must be located at a directory contained in exec-path'. (setq lsp-latex-texlab-jar-file 'search-from-exec-path) ;; If you want to put "texlab.jar" somewhere else, ;; you can specify the path to "texlab.jar" as follows: ;; (setq lsp-latex-texlab-jar-file "/path/to/texlab.jar") #+END_SRC

  • License This package is licensed by GPLv3. See [[file:LICENSE][LICENSE]].