macのvimでmarkdownを書いてLaTeXを経由して「論文」の注釈・引用・bibliographyつきのpdfを生成する
できるようになること
chromeでワンクリックで論文保存→執筆時に楽に挿入、bibliographyも自動作成
(大学の授業のレポートの引用文献を書くのがだるかったためやってみた)
手順
Tex環境の整備
brew cask install mactex
vim環境の整備
Zotero(文献管理してくれるGUIツール)のインストール
Zotero | Your personal research assistant
Downloadボタンを押し、インストール。
Chrome拡張もインストールすると、押すだけで論文を保存できるので便利。
citation.vimをインストール
GitHub - rafaqz/citation.vim: Zotero and bibtex citations for Vim
vimで引用アシストしてくれるツール。
- こんな感じに論文の候補が出てきて、選択するとkeyを挿入できる。コンパイルするとkeyは引用になり、(名前、年代)文章の最後にbibliographyが挿入される。
Unite.vimをインストールしていなかったので、これもインストールした。(Denite.vimには非対応らしい。)
GitHub - Shougo/unite.vim: Unite and create user interfaces
volt get rafaqz/citation.vim volt get Shougo/unite.vim
zoteroの設定
さっきインストールしたZoteroが自動でbibファイル(citation.vimが引用するのに参照するもの)を生成するようにbetter-bibtexをインストールする。
- 以下のurlから入ってインストール。
Release v5.2.22 · retorquere/zotero-better-bibtex · GitHub
zoteroを起動し、バーの「ツール」→「アドオン」でzotero-better-bibtexを選びインストール。
その後、バーの「Zotero」→「Better Bibtex」→「automatix Export」でautomatix exportの項目を「on charge」にする。
また、その下の項目でbibファイルを保存するディレクトリを選ぶ。
これで自動でbibファイルが生成される。
作成したbibファイルをvimrcでcitation.vimに認識されるよう指定する。(ここでは~/Desktop/report.bibを指定する)(.vimrcに追記)
let g:citation_vim_bibtex_file="~/Desktop/report.bib"
また、zoteroのディレクトリ位置を指定する。(.vimrcに追記)
let g:citation_vim_cache_path='~/.cache' let g:citation_vim_mode="zotero" let g:citation_vim_zotero_path="~/Zotero"
最後にuniteのプレフィクスを指定する。これで「space」+「u」で先ほどの画像のような引用文が出てきて、選択して挿入できる。(.vimrcに追記)
let mapleader = "\<Space>" nnoremap <Leader>o :CtrlP<CR> nnoremap <Leader>w :w<CR> nnoremap <leader>f :<C-u>Unite file<CR> nnoremap <leader>u :<C-u>Unite citation/key<CR>
あとはpdf作成を自動化できるようにlatexのテンプレートとスクリプトを書く。
- テンプレート
ほとんど
からの引用です
\documentclass[a4paper, uplatex, dvipdfmx]{jsreport} \usepackage{amsmath,amssymb} \usepackage[includeheadfoot,top=10truemm,bottom=10truemm, right=10truemm,left=10truemm]{geometry} % 余白を指定されている場合は geometry パッケージで調整 \usepackage{graphicx,color} \usepackage{hyperref} % 目次や図表番号に対してハイパーリンクを生成。hidelinks で PDF のハイパーリンクの枠を消す(参考: http://0-chromosome.hatenablog.jp/e ntry/2015/04/10/175912 ) \usepackage{pxjahyper} % ハイパーリンクに日本語が含まれる場合に必要 \usepackage{longtable} % 表組みに必要 \usepackage{booktabs} % 表組みに必要 \usepackage{subfig} % 図の横並び表示に必要 \usepackage[backend=biber]{biblatex} \addbibresource{~/Desktop/report.bib} \nocite{*} \newenvironment{cslreferences}% {}% {\par} \title{@@@@@} % タイトルなどはハードコーディングする必要あり \author{著者} \begin{document} \maketitle \input{testt} % ここに Pandoc で出力された LaTeX ファイルを挿入する % \printbibliography[title=参考文献] \end{document}
mdfile=$1 reportname=$2 outputfilename=$3 pandoc --filter pandoc-citeproc --bibliography=report.bib -M linkReferences --top-level-division=chapter $mdfile -o testt.tex sed "s:@@@@@:$reportname:g" report.tex > $outputfilename.tex latexmk $outputfilename
これによって
先頭に
--- bibliography: report.bib linkReferences: true ---
と書いたmarkdownを作ると、
bash makereport.sh [markdownファイル] [タイトル] [ファイル名]
で[ファイル名].pdfとしてコンパイルできる。
メモ
割と大変だった。他に楽にできる方法がないのか知りたい。