Skip to content

Instantly share code, notes, and snippets.

@radixm46
Last active August 7, 2021 06:21
Show Gist options
  • Save radixm46/735b9f05bf36da7bc3690e67e80b6ad4 to your computer and use it in GitHub Desktop.
Save radixm46/735b9f05bf36da7bc3690e67e80b6ad4 to your computer and use it in GitHub Desktop.
convert .tex from emacs org mode to docx with crossref(yaml), bibliography(bib) and csl using pandoc
#!/bin/bash
# convert .tex exported from emacs to .docx
# works with below:
# - pandoc 2.9.2.1
# - pandoc-citeproc 0.17
# - pandoc-crossref v0.3.6.2
cd $(dirname $0)/src
exp_docx_path=../docx/$(date +%Y%m%d%H%M).docx
source_tex=source.tex
prep_tex=source_prep.tex
csl_file=citation_format.csl
docx_format='templates/template.docx'
if [ ! -d '../docx' ]; then
mkdir ../docx
fi
# preprocess .tex
# table label to tbl, swap order of \caption and \label, remove zwsp
echo "processing ${source_tex}: START..."
perl -CSAD -0p -e \
's/\x{200B}//g; \
s/(\\(label|ref)\{)tab(:org[a-z0-9]{7,7}\})/$1tbl$3/g; \
s/(^\\caption\{)(\\label\{(tbl|fig):org[a-z0-9]{7,7}$\})(.*?\}\n)/$1\n$4$2/smg' \
${source_tex} > ${prep_tex}
echo "processing ${source_tex}: DONE"
pandoc \
-F pandoc-crossref\
-M "crossrefYaml=templates/pandoc-crossref.yaml"\
-F pandoc-citeproc\
--bibliography=reference.bib ${prep_tex}\
--csl templates/${csl_file}\
--reference-doc ${docx_format}\
-o ${exp_docx_path}
echo "exported: ${exp_docx_path}"
# reference file inside pandoc could acquired by below:
# pandoc --print-default-data-file reference.docx > template.docx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment