Skip to content

Instantly share code, notes, and snippets.

@wchargin
Created December 27, 2015 19:20
Show Gist options
  • Save wchargin/6d50b94106360693c74d to your computer and use it in GitHub Desktop.
Save wchargin/6d50b94106360693c74d to your computer and use it in GitHub Desktop.
adding math concealment bindings for TeX
" Excerpted from ~/.vim/after/ftplugin/tex.vim,
" which is sourced (executed) right after loading any TeX file
" (more precisely, any file with filetype=tex).
"
" These commands set up concealing for some math characters
" in addition to those defined throughout /usr/share/vim/vim74/syntax/tex.vim.
" See ':help conceal' for more information about concealment,
" and add 'set concealllevel=1' to your .vimrc to enable concealing.
"
" Test Plan:
" If you run the following command, you should see "-ℕ ℕ ℕ ℕ-":
" tabe test.tex | let @z='$\mathbb{N} \mathbb N \mB{N} \mB N$'."\n" | norm "zPG
" Function ConcealMath: Add a TeX math pretty-printing alias
" Usage: call ConcealMath('cmdName', 'replacement', optSuffix, optPrefix)
"
" The optional suffix defaults to '\>',
" which will force the end of the match to occur on a word boundary
" (so '\iso' will not match '\isosceles').
" (See ':help \>' for more information.)
" If this is a control symbol (like \:) instead of a control word (like \x),
" then this is not desirable; in this case, set the suffix to ''.
"
" The prefix defaults to the '\' required for a TeX macro;
" I don't know why you'd want to change this
" unless you're doing some black magic like \catcode`|=0
" which you almost certainly shouldn't be!
"
" Example: call ConcealMath('cdot', '·')
" Example: call ConcealMath(':', '_', '') " don't end at word boundary
"
function! ConcealMath(old, new, ...)
let l:suffix = (a:0 >= 1) ? a:1 : '\>'
let l:prefix = (a:0 >= 2) ? a:2 : '\\'
let l:match = "'" . l:prefix . a:old . l:suffix . "'"
exe "syntax match texMathSymbol " . l:match . " contained conceal cchar=" . a:new
endfunction
"
" Standard TeX/LaTeX commands
call ConcealMath('land', '∧')
call ConcealMath('lor', '∨')
call ConcealMath('lnot', '¬')
call ConcealMath('infty', '∞')
call ConcealMath(',', ' ', '') " thin space (control symbol: \,)
"
" Standard packages
call ConcealMath('implies', '⇒') " amssymb
call ConcealMath('impliedby', '⇐') " amssymb
call ConcealMath('iff', '⇔') " amssymb
call ConcealMath('rar', '→') " tikz-cd
call ConcealMath('lar', '←') " tikz-cd
"
" My custom commands
call ConcealMath('then', ':') " \forall x \then x^2 \geq 0
call ConcealMath('tuple', '◊') " \tuple{a, b} -> \langle a, b \rangle
call ConcealMath('iso', '≅') " alias for \cong (from amssymb)
call ConcealMath('maps', 'ℒ') " linear maps (for Hom-sets in Vec_k)
" Function ConcealMathbb: Add TeX pretty-printing aliases for blackboard bold.
" Usage: call ConcealMathbb('normalCharacter', 'doubleStruckCharacter')
"
" The best way to find the codepoints for the double-struck characters
" is to visit this webpage provided by the W3:
" http://www.w3.org/TR/MathML2/double-struck.html
"
" The above table highlights some of the rows in yellow because
" the double-struck characters are spread across two blocks of Unicode:
" a privileged few are in the Basic Multilingual Plane
" within the Letterlike Symbols block (U+2100 through U+214F);
" you can view those succinctly here:
" http://www.fileformat.info/info/unicode/block/letterlike_symbols/list.htm
" These are the yellow-highlighted rows in the table,
" and, when possible, it's always best to stay within the BMP.
"
" The ones that aren't in the BMP are listed consecutively
" from U+1D538 to U+1D56B, in Mathematical Alphanumerical Symbols.
" You can view those here:
" http://www.fileformat.info/info/unicode/block/mathematical_alphanumeric_symbols/list.htm
" This includes lowercase letters as well.
function! ConcealMathbb(old, new, alsoStandalone)
"
" Standard blackboard-bold from 'amsfonts'.
" These can be written as '\mathbb{N}x' or '\mathbb Nx'
" (each of which is equivalent to '\mathbb N x'),
" so the last character doesn't have to be a word boundary
" and we provide an empty string for the suffix.
call ConcealMath('mathbb{' . a:old . '}', a:new, '') " \mathbb{N}
call ConcealMath('mathbb ' . a:old, a:new, '') " \mathbb N
"
" My shortcut for \mathbb.
" Same suffix comment as above applies
" ('\mB{N}x' and '\mB Nx' are fine, and equivalent to '\mB N x').
call ConcealMath('mB{' . a:old . '}', a:new, '') " \mB{N}
call ConcealMath('mB ' . a:old , a:new, '') " \mB N
"
" My even shorter shortcuts for some letters.
if a:alsoStandalone
call ConcealMath(a:old, a:new) " standalone \N
endif
endfunction
"
" I've only included the ones I use.
" You can add more if you want.
call ConcealMathbb('R', 'ℝ', 1)
call ConcealMathbb('C', 'ℂ', 1)
call ConcealMathbb('Q', 'ℚ', 1)
call ConcealMathbb('Z', 'ℤ', 1)
call ConcealMathbb('N', 'ℕ', 1)
call ConcealMathbb('F', '𝔽', 1) " (arbitrary field)
" vim: ft=vim
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment