表格内的 pandoc 引用

表格内的 pandoc 引用

如何强制 pandoc 处理表格环境中使用的引用?一个简单的例子是:

\begin{table}[t]
\begin{tabular}{|l|l|} \hline  [@referencekey] & Description \\\hline
\end{tabular}
\end{table}

我使用以下 pandoc 命令进行编译

PANDOC_FLAGS=-s -N --template=template.tex -f markdown+yaml_metadata_block+tex_math_dollars -t latex
BIBLIO_FLAGS=--bibliography=mybib.bib --csl=acm.csl
YAML_METADATA=config.yaml
pandoc $(PANDOC_FLAGS) $(BIBLIO_FLAGS) $(YAML_METADATA) paper.mdtt

答案1

经过几个小时的思考,我终于解决了这个问题。关键思想是以某种方式获取 pandoc 可以使用的数字,如果它可以在 pandoc 处理后转换引用并在 .tex 中替换它们!

步骤如下

  1. 查找论文中所有引用的使用情况
  2. 在 markdown 末尾放置一个引用键 --> [@citation key] 的映射
  3. 运行 pandoc 来转换所有引用,包括我们的映射
  4. 读取论文中每个未转换的 [@citation key],并根据 .tex 末尾的映射进行替换
  5. 删除映射(可选,您可以将映射放在 \iffalse \fi 中以避免此步骤)

好的,现在这是我的代码:

PANDOC_FLAGS=-s -N --template=template.tex -f markdown+yaml_metadata_block+tex_math_dollars -t latex
BIBLIO_FLAGS=--bibliography=mybib.bib --csl=acm.csl
YAML_METADATA=config.yaml
NAME=paper

echo '\\iffalse' >> ${NAME}.mdtt
# find [@...] markdowns. Note that there can be multiple @... inside a bracket. We don't care, just replace the whole if they are not converted. 1) use sed to put each citation markdown in one line 2) find those lines using grep 3) remove the text after the citation markdowns 3) make the mapping. keys are put inside a table to not be converted using pandoc, values are just ordinary citation markdowns. I use 1::-- and 0::-- to recognize the map later in the .tex
sed ${NAME}.mdtt  -e 's/\(\[@\)/\n\1/g' |grep '\[[@a-zA-Z0-9_,\:\-\ ;]*\]'|  sed -e 's/.*\(\[[@a-zA-Z0-9_,\:\-\ ;]*\]\).*/\1/g' | sed -e 's/\[//g' -e 's/\]//g' | sed -e 's/\(.*\)/\\begin\{table\}1::-- \1\\end\{table\} 0::-- \[\1\]\n/g' >> ${NAME}.mdtt
echo '\\fi' >> ${NAME}.mdtt

pandoc $(PANDOC_FLAGS) $(BIBLIO_FLAGS) $(YAML_METADATA) $(NAME).mdtt footer.md > $(NAME).tex2

#bring them back. It needs another .sh file if you use it in a MakeFile because it seems that sed labeling doesn't work in MakeFiles
#put the mapping in a temp file, find the mapping using grep.  Sed expressions are to remove table environment, remove mapping arrow, concat lines (pandoc breaks lines after tables)!
grep '::--' ${NAME}.tex2 | sed -e 's/\\begin{table}1::-- \([@a-zA-Z0-9_,\:\-\ ;]*\)\\end{table}.*/\1/g' -e 's/0::-- //g' | sed -e ':a;N;$!ba;s/\(.\)\n\({\)/\1 \2/g' > temp.txt

cp ${NAME}.tex2 ${NAME}.tex;

#for each unresolved citation. Use grep to find lines with [@....], remove texts before and after it. remove [], 
for l in `sed ${NAME}.tex2  -e 's/\(\[@\)/\n\1/g' | grep '\[[@a-zA-Z0-9_,\:\-\ ;]*\]'|  sed -e 's/.*\(\[[@a-zA-Z0-9_,\:\-\ ;]*\]\).*/\1/g' | sed -e 's/\[//g' -e 's/\]//g' | grep '@'`;
do
       #find in the mapping and replace it
        v=`grep $l temp.txt | head -n 1 | cut -f 2- -d{`;
        sed -i  ${NAME}.tex -e "s/\[$l\]/{$v/g";
done
rm temp.txt

#run pdflatex here

相关内容