This blog post is about a book titled “LaTeX Beginner’s Guide - Book Review” written by Stefan Kottwitz
A month ago, I had to churn out a beamer deck and I was rusty on beamer syntax.
Went back to some very folders to read something on beamer and along the way
stumbled on to this book that I had earmarked to read many years ago. Had some
time on hands and hence managed to read the book cover to cover. I think if you
are starting LaTeX, this book will serve as a great introduction for you to get
started. There are enough code snippets that you can use to learn various
aspects of LaTeX. My intention was to rework on the emacs configuration to get
LaTeX and beamer documents out. My old emacs config had bugs and hence used this
book as motivator to sort out the config. Any other book would have served but i
found this book very easy to go over to test various emacs config options. After
spending a few hours, managed to get emacs config in place.
Here is my current config for LaTeX in emacs:
Emacs config for LaTeX
Insert item in a Latex environment
1
2
3
4
5
6
|
(setq TeX-PDF-mode t)
(with-eval-after-load 'latex
(define-key LaTeX-mode-map (kbd "M-RET") #'LaTeX-insert-item))
;; (eval-after-load 'latex
;; '(define-key LaTeX-mode-map (kbd "M-RET") (quote LaTeX-insert-item)))
(message "[Latexinit] Loaded: Insert item in a Latex enviroment")
|
Tex setting
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
;; AUCTeX core configuration
(setq-default TeX-master nil ;; Prompt for master file
TeX-PDF-mode t ;; Use PDF mode by default
TeX-parse-self t ;; Parse on load
TeX-auto-save t) ;; Parse on save
<span class="c1">;; Enable folding and math input mode in LaTeX buffers</span>
<span class="p">(</span><span class="nv">add-hook</span> <span class="ss">'LaTeX-mode-hook</span>
<span class="p">(</span><span class="nb">lambda</span> <span class="p">()</span>
<span class="p">(</span><span class="nv">TeX-fold-mode</span> <span class="mi">1</span><span class="p">)</span>
<span class="p">(</span><span class="nv">LaTeX-math-mode</span><span class="p">)))</span>
(setq doc-view-ghostscript-program "C:/Program Files/gs/gs10.00.0/bin/gswin64.exe")
(setq preview-gs-command "C:/Program Files/gs/gs10.00.0/bin/gswin64.exe")
(setq preview-image-type 'dvisvgm)
(message "[Latexinit] Loaded: Tex setting")
|
Spell check
1
2
3
4
5
|
;; Enable Flyspell mode for LaTeX (AUCTeX)
(add-hook 'LaTeX-mode-hook #'flyspell-mode)
;; Set default dictionary to English
(setq ispell-dictionary "english")
(message "[Latexinit] Loaded: Spell check")
|
Preview mode
1
2
|
(add-hook 'doc-view-mode-hook 'auto-revert-mode)
(message "[Latexinit] Loaded: Preview Mode")
|
Docview config
1
2
3
4
5
6
7
8
9
|
(use-package doc-view
:defer t
:custom
;; Use MikTeX's utilities for PDF conversion and searching
(doc-view-ghostscript-program "C:/Users/radha/AppData/Local/Programs/MiKTeX/miktex/bin/x64/mgs.exe")
(doc-view-pdf->png-converter-function 'doc-view-pdf->png-converter-ghostscript)
(doc-view-pdftotext-program "miktex-pdftotext.exe")
)
(message "[Latexinit] Loaded: Doc view config")
|
Docview
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
(with-eval-after-load "doc-view"
(easy-menu-define my-doc-view-menu doc-view-mode-map "Menu for Doc-View Mode."
'("DocView"
["Switch to a different mode" doc-view-toggle-display :help "Switch to a different mode"]
["Open Text" doc-view-open-text :help "Display the current doc's contents as text"]
"--"
("Navigate Doc"
["Goto Page ..." doc-view-goto-page :help "View the page given by PAGE"]
"--"
["Scroll Down" doc-view-scroll-down-or-previous-page :help "Scroll page down ARG lines if possible, else goto previous page"]
["Scroll Up" doc-view-scroll-up-or-next-page :help "Scroll page up ARG lines if possible, else goto next page"]
"--"
["Next Line" doc-view-next-line-or-next-page :help "Scroll upward by ARG lines if possible, else goto next page"]
["Previous Line" doc-view-previous-line-or-previous-page :help "Scroll downward by ARG lines if possible, else goto previous page"]
("Customize"
["Continuous Off"
(setq doc-view-continuous nil)
:help "Stay put in the current page, when moving past first/last line" :style radio :selected
(eq doc-view-continuous nil)]
["Continuous On"
(setq doc-view-continuous t)
:help "Goto to the previous/next page, when moving past first/last line" :style radio :selected
(eq doc-view-continuous t)]
"---"
["Save as Default"
(customize-save-variable 'doc-view-continuous doc-view-continuous)
t])
"--"
["Next Page" doc-view-next-page :help "Browse ARG pages forward"]
["Previous Page" doc-view-previous-page :help "Browse ARG pages backward"]
"--"
["First Page" doc-view-first-page :help "View the first page"]
["Last Page" doc-view-last-page :help "View the last page"])
"--"
("Adjust Display"
["Enlarge" doc-view-enlarge :help "Enlarge the document by FACTOR"]
["Shrink" doc-view-shrink :help "Shrink the document"]
"--"
["Fit Width To Window" doc-view-fit-width-to-window :help "Fit the image width to the window width"]
["Fit Height To Window" doc-view-fit-height-to-window :help "Fit the image height to the window height"]
"--"
["Fit Page To Window" doc-view-fit-page-to-window :help "Fit the image to the window"]
"--"
["Set Slice From Bounding Box" doc-view-set-slice-from-bounding-box :help "Set the slice from the document's BoundingBox information"]
["Set Slice Using Mouse" doc-view-set-slice-using-mouse :help "Set the slice of the images that should be displayed"]
["Set Slice" doc-view-set-slice :help "Set the slice of the images that should be displayed"]
["Reset Slice" doc-view-reset-slice :help "Reset the current slice"])
("Search"
["New Search ..."
(doc-view-search t)
:help "Jump to the next match or initiate a new search if NEW-QUERY is given"]
"--"
["Search" doc-view-search :help "Jump to the next match or initiate a new search if NEW-QUERY is given"]
["Backward" doc-view-search-backward :help "Call `doc-view-search' for backward search"]
"--"
["Show Tooltip" doc-view-show-tooltip :help nil])
("Maintain"
["Reconvert Doc" doc-view-reconvert-doc :help "Reconvert the current document"]
"--"
["Clear Cache" doc-view-clear-cache :help "Delete the whole cache (`doc-view-cache-directory')"]
["Dired Cache" doc-view-dired-cache :help "Open `dired' in `doc-view-cache-directory'"]
"--"
["Revert Buffer" doc-view-revert-buffer :help "Like `revert-buffer', but preserves the buffer's current modes"]
"--"
["Kill Proc" doc-view-kill-proc :help "Kill the current converter process(es)"]
["Kill Proc And Buffer" doc-view-kill-proc-and-buffer :help "Kill the current buffer"])
"--"
["Customize"
(customize-group 'doc-view)]))
(easy-menu-define my-doc-view-minor-mode-menu doc-view-minor-mode-map "Menu for Doc-View Minor Mode."
'("DocView*"
["Display in DocView Mode" doc-view-toggle-display :help "View"]
["Exit DocView Mode" doc-view-minor-mode])))
|
1
2
3
4
5
6
7
8
9
|
(use-package pdf-tools
:pin manual
:config
(pdf-tools-install)
(setq-default pdf-view-display-size 'fit-width)
(define-key pdf-view-mode-map (kbd "C-s") 'isearch-forward)
:custom
(pdf-annot-activate-created-annotations t "automatically annotate highlights"))
(message "[Latexinit] Loaded: PDF Tools default config")
|
Org mode default PDF app
1
2
3
4
5
6
|
(with-eval-after-load 'org
(add-to-list 'auto-mode-alist
'("\\.pdf\\'" . pdf-view-mode))
(delete '("\\.pdf\\'" . default) org-file-apps)
(add-to-list 'org-file-apps '("\\.pdf\\'" . emacs)))
(message "[Latexinit] Loaded: Org Mode default pdf app")
|
Company mode for auctex
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
|
(use-package company
:ensure t
:config
(global-company-mode))
(use-package auctex
:ensure t)
(use-package company-auctex
:ensure t
:after (company auctex)
:config
(company-auctex-init))
(defun my/company-auctex-setup ()
(setq-local company-backends
(append
'((company-auctex-labels
company-auctex-bibs
company-auctex-macros
company-auctex-environments
company-auctex-symbols))
company-backends)))
(add-hook 'LaTeX-mode-hook #'my/company-auctex-setup)
(message "[Latexinit] Loaded: Company Mode for auctex")
|
Yassnippet
1
2
3
4
5
6
|
(use-package yasnippet
:ensure t
:hook (LaTeX-mode . yas-minor-mode)
:config
(yas-reload-all))
(message "[Latexinit] Loaded: Yasnippet")
|
SyncTex is enabled
1
2
3
4
5
6
7
8
9
|
(setq TeX-source-correlate-mode t)
(setq TeX-source-correlate-start-server t)
(setq TeX-view-program-list '(("PDF Tools" TeX-pdf-tools-sync-view)))
(setq TeX-view-program-selection '((output-pdf "PDF Tools")))
(add-hook 'LaTeX-mode-hook #'TeX-source-correlate-mode)
(setq TeX-command-extra-options "-synctex=1")
(message "[Latexinit] Loaded: SyncTex enabled")
|
Smart Toggle
Inline images are not displayed when TeX-PDF-mode is not nil and hence when you
want images, toggle the setting
1
2
3
4
5
6
7
|
(defun toggle-TeX-PDF-mode ()
"Toggle between TeX DVI mode and PDF mode."
(interactive)
(setq TeX-PDF-mode (not TeX-PDF-mode))
(message "TeX-PDF-mode is now %s" (if TeX-PDF-mode "ON (PDF)" "OFF (DVI)")))
(define-key LaTeX-mode-map (kbd "C-c q") 'toggle-TeX-PDF-mode)
(message "[Latexinit] Loaded: Smart toggle enabled")
|
For inline images press C-c q For preview toggle back and see the live pdf
Inverse search
1
2
3
4
|
(with-eval-after-load 'pdf-tools
(require 'pdf-sync)
(define-key pdf-view-mode-map (kbd "C-c C-s") #'pdf-sync-backward-search))
(message "[Latexinit] Loaded: Inverse Search enabled")
|
Narrow and widening
#+begin_src emacs-lisp
Outline Minor Mode and Narrowing
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
|
(message "[Latexinit] Loaded: Outline narrowing for LaTeX sections")
(defun rk/narrow-to-latex-section ()
"Narrow to current LaTeX section or subsection using outline."
(interactive)
(unless (derived-mode-p 'latex-mode)
(error "Not in a LaTeX buffer"))
(save-excursion
(outline-back-to-heading t)
(let ((start (point)))
(outline-end-of-subtree)
(narrow-to-region start (point)))))
(defun rk/setup-latex-outline ()
"Enable outline-minor-mode and set up LaTeX sectioning."
(outline-minor-mode 1)
(setq-local outline-regexp
"\\\(part\|chapter\|section\|subsection\|subsubsection\|paragraph\|subparagraph\)\*?{")
(setq-local outline-level
(lambda ()
(let ((match (match-string 1)))
(cond
((equal match "part") 1)
((equal match "chapter") 2)
((equal match "section") 3)
((equal match "subsection") 4)
((equal match "subsubsection") 5)
((equal match "paragraph") 6)
((equal match "subparagraph") 7)
(t 8)))))
;; Bind C-x n s to narrow to current section
(define-key LaTeX-mode-map (kbd "C-x n s") #'rk/narrow-to-latex-section))
(add-hook 'LaTeX-mode-hook #'rk/setup-latex-outline)
|
Auto refresh pane not working. Hence setting it in the global mode
1
2
3
|
(global-auto-revert-mode 1)
(setq global-auto-revert-non-file-buffers t)
|
Smart latex option
1
2
3
4
5
6
|
(with-eval-after-load 'tex
(add-to-list 'TeX-command-list
'("LatexMk" "latexmk -pdf -interaction=nonstopmode %s"
TeX-run-TeX nil t
:help "Run LatexMk"))
(setq TeX-command-default "LatexMk"))
|