Vim > Vim-LaTeX (LaTeX-Suite)

Vim-LaTeX (LaTeX-Suite) → 問題が発生する場合は VimTeX を使用する



Vim-LaTeX とは

Vim-LaTeX は Vim で LaTeX ドキュメントを編集,コンパイル,プレビューするためのプラグインです.

License

VIM ライセンス

ChangeLog

不具合

MacVim-KaoriYa を使用して Vim-LaTeX でノーマルモードに入る時に自動で IME がオフになる機能を有効にするとインサートモードでの日本語入力がおかしくなる → VimでNormalモード切り替え時にIMEをOFFにする、をMacでKarabiner-Elementsを使って実現する

MacVim-KaoriYa で Vim-LaTeX を使用すると日本語入力が中断される → $HOME/.vim/gvimrc に set imdisable を記述して MacVim メニューの「環境設定」「詳細」の「インラインインプットメソッドを使用する」のチェックをはずす

Linux の GVim で Vim-LaTeX を使用する場合に発生する日本語入力に関する不具合 → ソースコードの修正またはコンソール版の Vim を使用する

Linux の GVim で Vim-LaTeX を使用すると日本語入力に問題が発生します.
GVim の on-the-spot の実装に問題があるようです.

CTRL-J が使えない → https://github.com/vim-jp/issues/issues/523#issuecomment-35015892

\int や {\pi} という表示が白く変換して表示される → https://github.com/vim-jp/issues/issues/529#issuecomment-35566960

インストール

dein.vim を使用して GitHub の vim-latex からパッケージをインストールする場合

dein.vim を使って Vim-LaTeX をインストールできます.

call dein#add('vim-latex/vim-latex')

dein.vim を使用せずに GitHub の vim-latex からパッケージをインストールする場合

https://github.com/vim-latex/vim-latex/releases からリリース版あるいは https://github.com/vim-latex/vim-latex/archive/master.zip から最新版をダウンロードします.

Windows の場合

Vim を起動し

 :echo $HOME

で,$HOME の場所を確認します.

パッケージを展開し,$HOME/vimfiles/ に

を配置します.

macOS, Linux の場合

Vim を起動し

 :echo $HOME

で,$HOME の場所を確認します.

パッケージを展開し,$HOME/.vim/ に

を配置します.

設定

Windows

$HOME/vimfiles/vimrc に設定を記述します.


""
"" Vim-LaTeX
""
filetype plugin on
filetype indent on
set shellslash
set grepprg=grep\ -nH\ $*
let g:tex_flavor='latex'
let g:Imap_UsePlaceHolders = 1
let g:Imap_DeleteEmptyPlaceHolders = 1
let g:Imap_StickyPlaceHolders = 0
let g:Tex_DefaultTargetFormat = 'pdf'
let g:Tex_MultipleCompileFormats='dvi,pdf'
"let g:Tex_FormatDependency_pdf = 'pdf'
let g:Tex_FormatDependency_pdf = 'dvi,pdf'
let g:Tex_CompileRule_pdf = 'ptex2pdf -u -l -ot "-kanji=utf8 -no-guess-input-enc -synctex=1 -interaction=nonstopmode -file-line-error-style" $*'
"let g:Tex_CompileRule_pdf = 'lualatex -synctex=1 -interaction=nonstopmode -file-line-error-style $*'
let g:Tex_CompileRule_dvi = 'uplatex -kanji=utf8 -no-guess-input-enc -synctex=1 -interaction=nonstopmode -file-line-error-style $*'
let g:Tex_BibtexFlavor = 'bibtexu'
let g:Tex_MakeIndexFlavor = 'upmendex $*.idx'
let g:Tex_ViewRule_pdf = 'rundll32 shell32,ShellExec_RunDLL SumatraPDF -reuse-instance -inverse-search "\"' . $VIM . '\gvim.exe\" -n -c \":RemoteOpen +\%l \%f\""'
"let g:Tex_ViewRule_pdf = 'texworks'
"let g:Tex_ViewRule_pdf = 'rundll32 shell32,ShellExec_RunDLL firefox -new-window'
"let g:Tex_ViewRule_pdf = 'powershell -Command "& {$p = [System.String]::Concat(''"""'',[System.IO.Path]::GetFullPath($args),''"""'');Start-Process chrome -ArgumentList (''--new-window'',$p)}"'
"let g:Tex_ViewRule_pdf = 'rundll32 shell32,ShellExec_RunDLL AcroRd32'

SumatraPDF の inverse search については Vim-LaTeX#SumatraPDF との連携 (inverse search) を参照してください.

macOS

動作未確認です.

$HOME/.vim/vimrc に設定を記述します.


""
"" Vim-LaTeX
""
filetype plugin on
filetype indent on
set shellslash
set grepprg=grep\ -nH\ $*
let g:tex_flavor='latex'
let g:Imap_UsePlaceHolders = 1
let g:Imap_DeleteEmptyPlaceHolders = 1
let g:Imap_StickyPlaceHolders = 0
let g:Tex_DefaultTargetFormat = 'pdf'
let g:Tex_MultipleCompileFormats='dvi,pdf'
"let g:Tex_FormatDependency_pdf = 'pdf'
let g:Tex_FormatDependency_pdf = 'dvi,pdf'
let g:Tex_CompileRule_pdf = 'ptex2pdf -u -l -ot "-synctex=1 -interaction=nonstopmode -file-line-error-style" $*'
"let g:Tex_CompileRule_pdf = 'lualatex -synctex=1 -interaction=nonstopmode -file-line-error-style $*'
let g:Tex_CompileRule_dvi = 'uplatex -synctex=1 -interaction=nonstopmode -file-line-error-style $*'
let g:Tex_BibtexFlavor = 'bibtexu'
let g:Tex_MakeIndexFlavor = 'upmendex $*.idx'
let g:Tex_UseEditorSettingInDVIViewer = 1
let g:Tex_ViewRule_pdf = 'Skim'
"let g:Tex_ViewRule_pdf = 'open -a Skim'
"let g:Tex_ViewRule_pdf = 'open -a Preview'
"let g:Tex_ViewRule_pdf = 'open -a TeXShop'
"let g:Tex_ViewRule_pdf = '/Applications/TeXworks.app/Contents/MacOS/TeXworks'
"let g:Tex_ViewRule_pdf = '/Applications/texstudio.app/Contents/MacOS/texstudio --pdf-viewer-only'
"let g:Tex_ViewRule_pdf = 'open -a Firefox'
"let g:Tex_ViewRule_pdf = 'open -a "Adobe Acrobat Reader DC"'
"let g:Tex_ViewRule_pdf = 'open'

日本語入力に不具合が発生する場合は $HOME/.vim/gvimrc に set imdisable を記述して MacVim メニューの「環境設定」「詳細」の「インラインインプットメソッドを使用する」のチェックをはずします.


""
"" Vim-LaTeX
""
set imdisable

set imdisable を設定すると問題が発生する場合は set imdisable を削除してください.


bibtexを使うなど,複数回のコンパイルが必要な場合は,「g:Tex_MultipleCompileFormats」オプションを設定する必要があります.
例えば,pdfでbibtexを使う場合は,

let g:Tex_MultipleCompileFormats = 'pdf'

という設定を書き加えます.
参考:http://superuser.com/questions/257007/macvim-wont-run-bibtex-or-compile-as-necessary-to-clear-warnings-with-ll-sho

Linux

$HOME/.vim/vimrc に設定を記述します.


""
"" Vim-LaTeX
""
filetype plugin on
filetype indent on
set shellslash
set grepprg=grep\ -nH\ $*
let g:tex_flavor='latex'
let g:Imap_UsePlaceHolders = 1
let g:Imap_DeleteEmptyPlaceHolders = 1
let g:Imap_StickyPlaceHolders = 0
let g:Tex_DefaultTargetFormat = 'pdf'
let g:Tex_MultipleCompileFormats='dvi,pdf'
"let g:Tex_FormatDependency_pdf = 'pdf'
let g:Tex_FormatDependency_pdf = 'dvi,pdf'
let g:Tex_CompileRule_pdf = 'ptex2pdf -u -l -ot "-synctex=1 -interaction=nonstopmode -file-line-error-style" $*'
"let g:Tex_CompileRule_pdf = 'lualatex -synctex=1 -interaction=nonstopmode -file-line-error-style $*'
let g:Tex_CompileRule_dvi = 'uplatex -synctex=1 -interaction=nonstopmode -file-line-error-style $*'
let g:Tex_BibtexFlavor = 'bibtexu'
let g:Tex_MakeIndexFlavor = 'upmendex $*.idx'
let g:Tex_UseEditorSettingInDVIViewer = 1
let g:Tex_ViewRule_pdf = 'xdg-open'
"let g:Tex_ViewRule_pdf = 'evince'
"let g:Tex_ViewRule_pdf = 'okular --unique'
"let g:Tex_ViewRule_pdf = 'zathura -x "vim --servername synctex -n --remote-silent +\%{line} \%{input}"'
"let g:Tex_ViewRule_pdf = 'qpdfview --unique'
"let g:Tex_ViewRule_pdf = 'texworks'
"let g:Tex_ViewRule_pdf = 'mupdf'
"let g:Tex_ViewRule_pdf = 'firefox -new-window'
"let g:Tex_ViewRule_pdf = 'chromium --new-window'

Evince の forward and inverse search は Vim-LaTeX#Evince との連携 (forward and inverse search) を参照してください.

使い方

詳細は VIM-LaTeX Manual を参照してください.

折りたたみ

デフォルトでは TeX ファイルを開くと折りたたまれた状態で表示されます.


 welcome home : vim online                                         vim.tex - GVIM1                                 [ 🗕 ][ 🗖 ][ 🗙 ]
  ファイル(F)  編集(E)  ツール(T)  シンタックス(S)  バッファ(B)  ウィンドウ(W)  TeX-Suite  TeX-Environments  TeX-Elements  TeX-Math  ヘルプ(H)   
%
+--  7 行: Preamble: \documentclass[uplatex,dvipdfmx,12pt,ja=standard]{bxjsarticle}-------------------------------------------------------------
\begin{document}
+-- 46 行:\section{Vim について}-------------------------------------------------------------------------------------------------------
\end{document}


TeX ファイルを開くときに自動的に折りたたみを行う機能を off にするには $HOME/vimfiles/vimrc または $HOME/.vim/vimrc に

let g:Tex_AutoFolding = 0

を追加します.

カーソル位置の折りたたみを開く場合は <Space> で代用できます.

Vim-LaTeX では,折りたたみに関しては以下の 2 つの操作を覚えていれば OK です.

メニュー [TeX-Suite]ノーマルモード機能
<Space>カーソル位置の折りたたみを開いてカーソルを1つ進める
Refresh Folds\rfカーソル位置以外の折りたたみを閉じる

Vim#使い方#折りたたみ も参照してください.

コンパイル・プレビュー・検索

メニュー [TeX-Suite]ノーマルモード機能
Compile\llコンパイル
View\lvプレビュー
Search\ls検索 (forward search)

\ll, \lv, \ls は素早く入力する必要があります.

プレビューはファイルの自動更新機能をサポートしているビューアを使用します.
\ll でコンパイルして \lv でビューアを起動すれば,その後は \lv をもう一度タイプしなくても OK です.
TeX ファイルを保存して \ll でコンパイルすれば,自動的に再描画してくれます.

コンパイル・プレビュー対象フォーマットの変更

メニュー [TeX-Suite]コマンドラインモード機能
Target Format:TTarget pdfコンパイル・プレビュー対象フォーマットを PDF に変更

編集中にコンパイル・プレビュー対象フォーマットを変更することができます.

macOS ではデフォルトの対象フォーマットは PDF です.その他の OS ではデフォルトの対象フォーマットは DVI です.

デフォルトの対象フォーマットを PDF に変更する場合は $HOME/vimfiles/vimrc または $HOME/.vim/vimrc に

let g:Tex_DefaultTargetFormat = 'pdf'

を追加します.

補完

Environment Mappings

メニュー [TeX-Environments]インサートモードビジュアルモード補完 (インサートモード)補完 (ビジュアルモード)
Structure -> documentEDO
document <F5>
<F5> document
,do
<F5> document
\documentclass[<+options+>]{<+class+>}

\begin{document}
<++>
\end{document}
\begin{document}
...
\end{document}
Lists -> itemizeEIT
itemize <F5>
<F5> itemize
,it
<F5> itemize
\begin{itemize}
\item
\end{itemize}<++>
\begin{itemize}
...
\end{itemize}

インサートモードの状態で,EDO とタイプすると

\documentclass[<+options+>]{<+class+>}

\begin{document}
<++>
\end{document}

と補完されます.

<+options+>, <+class+>, <++> は place-holder で,<Ctrl-J> をタイプすると place-holder が表示されている場所へジャンプします.

EDO を表示したい場合は,EDo をタイプして <Esc> (<Ctrl-[> または <Ctrl-C> でも OK) をタイプして ~ をタイプします.

インサートモードの状態で,EIT とタイプすると

\begin{itemize}
  \item 
\end{itemize}<++>

と補完されます.

2つ目以降の \item は <Alt-I> で補完できます.

<Ctrl-J> をタイプすると <++> へジャンプします.

EIT を表示したい場合は,EI をタイプして <Ctrl-V> をタイプして T をタイプします.

Command Mappings

ノーマルモードインサートモードビジュアルモード補完 (ノーマルモード・インサートモード)補完 (ビジュアルモード)
frac にカーソルがある状態で <F7>
<F7> frac
frac <F7>
<F7> frac
\frac{<+n+>}{<+d+>}<++>
footnote にカーソルがある状態で <F7>
<F7> footnote
footnote <F7>
<F7> footnote
<F7> footnote\footnote{}<++>\footnote{
...
}

インサートモードの状態で,frac <F7> とタイプすると

\frac{<+n+>}{<+d+>}<++>

と補完されます.

<Ctrl-J> をタイプすると <+n+>, <+d+>, <++> へジャンプします.

Font Mappings

メニュー [TeX-Elements]インサートモードビジュアルモード補完 (インサートモード)補完 (ビジュアルモード(v))補完 (ビジュアルモード(<Shift-V>, <Ctrl-V>))
Font -> Family -> rmfamilyFRM`rm\textrm{}<++>\textrm{...}{\rmfamily
...
}

インサートモードの状態で,FRM とタイプすると

\textrm{}<++>

と補完されます.

<Ctrl-J> をタイプすると <++> へジャンプします.

FRM を表示したい場合は,FR をタイプして <Ctrl-V> をタイプして M をタイプします.

Section Mappings

メニュー [TeX-Environments]インサートモードビジュアルモード補完 (インサートモード)補完 (ビジュアルモード)
Sections -> sectionSSE,se\section{}<++>\section{...}

インサートモードの状態で,SSE とタイプすると

\section{}<++>

と補完されます.

<Ctrl-J> をタイプすると <++> へジャンプします.

SSE を表示したい場合は,SS をタイプして <Ctrl-V> をタイプして E をタイプします.

Greek Letter Mappings

メニュー [TeX-Math]インサートモード補完
Greek -> Small -> alpha`a\alpha

インサートモードの状態で,`a とタイプすると

\alpha

と補完されます.

`a を表示したい場合は,` をタイプして <Ctrl-V> をタイプして a をタイプします.

Smart Key Mappings

インサートモード補完
"``

vim-latexで引用符を直に入力する方法 を参照.

デフォルトではインサートモードの状態で," を入力すると

``

と補完されます.

" を表示したい場合は,<Ctrl-V> をタイプして " をタイプします.

Smart Quotes を off にするには $HOME/vimfiles/vimrc または $HOME/.vim/vimrc に

let g:Tex_SmartKeyQuote = 0

を追加します.

Auc-Tex Key Bindings

インサートモード補完
`/\frac{}{<++>}<++>

インサートモードの状態で,`/ とタイプすると

\frac{}{<++>}<++>

と補完されます.

<Ctrl-J> をタイプすると <++> へジャンプします.

`/ を表示したい場合は,` をタイプして <Ctrl-V> をタイプして / をタイプします.

マクロの展開を抑制

EIT などのマクロの展開を抑制したい場合は,

コマンドラインモードで

:let b:Imap_FreezeImap=1

とします.元に戻す場合は

:let b:Imap_FreezeImap=0

とします.

SumatraPDF との連携 (forward search)

SumatraPDF で forward search を使用するには LaTeX コマンドに -synctex=1 を追加して

$HOME/vimfiles/vimrc の g:Tex_ViewRule_pdf を Vim-LaTeX#SumatraPDF との連携 (inverse search) を参照して設定します.

使い方は

  1. GVim を起動して TeX ファイルを開きます.
  2. :TTarget pdf
  3. \ll でコンパイルして PDF ファイルを作成します.
  4. \ls で SumatraPDF で PDF ファイルをプレビューして forward search を行います.

SumatraPDF を前面に表示したい場合

$HOME/vimfiles/ftplugin/latex-suite/compiler.vim を修正します.



...
"      For inverse search, if you are reading this, then just pressing \ls
"      will work.
function! Tex_ForwardSearchLaTeX()
...
  " tips on vim.sf.net (vim.sf.net tip #225)
  if (has('win32') && (viewer =~? '^ *yap\( \|$\)'))

    let execString = 'silent! !start '. viewer.' -s '.line('.').expand('%').' '.mainfnameRoot

  elseif (has('win32') && (viewer =~? "sumatrapdf"))

    let relativeFile = substitute(expand("%:p"), Tex_GetMainFileName(':p:h').'/', '','')
    let execString = 'silent! !start ' . viewer . ' -reuse-instance "' .mainfnameFull . '.' . s:target .'" -forward-search "' . relativeFile . '" ' . line('.')

...

SumatraPDF を前面に表示したくない場合

SumatraPDF/fwdsumatrapdf の fwdsumatrapdf.exe を使用します.

$HOME/vimfiles/ftplugin/latex-suite/compiler.vim を修正します.



...
"      For inverse search, if you are reading this, then just pressing \ls
"      will work.
let s:path = expand('<sfile>:p:h')
function! Tex_ForwardSearchLaTeX()
...
  " tips on vim.sf.net (vim.sf.net tip #225)
  if (has('win32') && (viewer =~? '^ *yap\( \|$\)'))

    let execString = 'silent! !start '. viewer.' -s '.line('.').expand('%').' '.mainfnameRoot

  elseif (has('win32') && (viewer =~? "sumatrapdf"))

    let relativeFile = substitute(expand("%:p"), Tex_GetMainFileName(':p:h').'/', '','')
    let execString = 'silent! !fwdsumatrapdf.exe '.'"'.mainfnameFull.'.'.s:target.'" "'.relativeFile.'" '. line('.')

...

SumatraPDF との連携 (inverse search)

SumatraPDF で inverse search を使用するには LaTeX コマンドに -synctex=1 を追加します.
Vim-LaTeX で SumatraPDF の inverse search をする場合は,tex.vim, quickrun, LaTeX Box の設定とは異なる記述が必要です.

Vim-LaTeX の場合 inverse search は SumatraPDF の [☰]-[設定(S)]-[オプション(O)...] の逆順検索コマンドラインの設定に

"C:\vim\gvim.exe" -n -c ":RemoteOpen +%l %f"

です.
設定ファイルから SumatraPDF の -inverse-search コマンドラインオプションで指定することもできます.
gvim.exe が :echo $VIM で表示されるディレクトリに存在する場合 $HOME/vimfiles/vimrc の g:Tex_ViewRule_pdf を


let g:Tex_ViewRule_pdf = 'rundll32 shell32,ShellExec_RunDLL SumatraPDF -reuse-instance -inverse-search "\"' . $VIM . '\gvim.exe\" -n -c \":RemoteOpen +\%l \%f\""'

に設定します.

SumatraPDF の inverse search は ダブルクリックで行います.

Evince との連携 (forward and inverse search)

evince で forward and inverse search を使用するには LaTeX コマンドに -synctex=1 を追加して

$HOME/.vim/vimrc の g:Tex_ViewRule_pdf を


let g:Tex_ViewRule_pdf = 'evince'

に設定します.

$HOME/.vim/ftplugin/latex-suite/compiler.vim を修正します.
fwdevince は Evince/fwdevince の fwdevince を使用します.



function! Tex_ForwardSearchLaTeX()
...
  else
    " We're either UNIX or Mac and using a UNIX-type viewer
...
    else
      " We must be using a generic UNIX viewer
      " syntax is: viewer TARGET_FILE LINE_NUMBER SOURCE_FILE

      if viewer =~? "evince"

        let execString = 'silent! !fwdevince' .' '. fnamemodify(Tex_GetMainFileName(), ":p:r") .'.pdf '. line(".") .' '. expand("%:p")

      else

        let execString = 'silent! !'.viewer.' "'.mainfnameRoot.'.'.s:target.'" '.line('.').' "'.expand('%').'"'

      endif

    endif

evince の forward search は

  1. Vim を起動して TeX ファイルを開きます.
  2. :TTarget pdf
  3. \ll でコンパイルして PDF ファイルを作成します.
  4. \ls で evince で PDF ファイルをプレビューして forward search を行います.

evince で inverse search をする場合は

$ vim --servername hoge hoge.tex

または

$ gvim --servername hoge hoge.tex

として起動します.

evince の inverse search は Ctrl + 左クリックで行います.

evince_backward_search を使って inverse search を行う場合は

$ evince_backward_search hoge.pdf "vim --servername hoge -n --remote-silent '+%l<Enter>' %f"

として evince_backward_search を起動すると evince から inverse search ができます.

行をハイライトしたい場合は

$ evince_backward_search hoge.pdf "vim --servername hoge -n --remote-silent '+%l<Enter>:match Search /\%%ll/' %f"

とします.

関連サイト


Last-modified: 2023-12-17 (日) 04:43:52 (125d)