見出しのテキストの中にある生のLaTeXのインラインの文字列が、
Pandocが自動挿入する\texorpdfstring
の中でもそのまま使われるようになる。
\CID{1481}
{=latex}城市でも\CID{8218}
{=latex}は素敵
#!/bin/bash | |
pandoc test.md --lua-filter header_latex.lua -o test.tex | |
uplatex -halt-on-error testmain || exit | |
uplatex -halt-on-error testmain || exit | |
dvipdfmx testmain || exit | |
rm testmain.dvi testmain.aux testmain.log |
-- header_latex.lua | |
-- | |
-- @copyright 2019 Takayuki YATO (aka. "ZR") | |
-- GitHub: https://github.com/zr-tex8r | |
-- Twitter: @zr_tex8r | |
-- This program is distributed under the MIT License. | |
-- | |
local filter_name = 'header_latex' | |
---------------------------------------- 設定 | |
--- デバッグログを出すか | |
local show_log = true | |
---------------------------------------- 補助 | |
local insert, remove = table.insert, table.remove | |
--- デバッグログを出力する. | |
local function log(fmt, ...) | |
if not show_log then return end | |
io.stderr:write(filter_name..': '..fmt:format(...).."\n") | |
end | |
---------------------------------------- フィルタ用補助 | |
--- LaTeXコード | |
local LATEX_PROLOGUE = [==[ | |
\def\PRLTeX#1#2{#1} | |
\def\PRLPdf#1#2{\texorpdfstring{}{#1{#2}}} | |
\def\PRLSubst#1#2#3{\PRLSubstA#3\PRLEnd{#2}{#1}} | |
\def\PRLSubstA#1/?PRL?/#2\PRLEnd#3#4{#4{#1#3#2}} | |
\def\PRLFinish#1{#1} | |
]==] | |
local MARK = '\tMARK' | |
local RX_MARK = MARK..'$' | |
--- マーク付きのRawInline. | |
local function marked_raw_inline(format, text) | |
return pandoc.RawInline(format..MARK, text) | |
end | |
--- RawInlineのマークを外す. | |
local function unmark_raw_inline(elem) | |
if elem.t ~= 'RawInline' then return end | |
elem.format = elem.format:gsub(RX_MARK, '') | |
end | |
--- マーク無しのlatexのRawInlineであるか. | |
local function is_unmarked_raw_latex(elem) | |
return elem.format == 'latex' | |
end | |
--- マーク有りのlatexのRawInlineであるか. | |
local function is_marked_raw_latex(elem) | |
return elem.format == 'latex'..MARK | |
end | |
---------------------------------------- フェーズ'convert' | |
local convert_count = 0 | |
local prl_label = '/?PRL?/' | |
local function convert_raw_latex(raw, rawtexts) | |
if is_unmarked_raw_latex(raw) then | |
convert_count = convert_count + 1 | |
log('process raw LaTeX: %s', raw.text) | |
insert(rawtexts, raw.text) | |
local text1 = ([[\PRLTeX{%s}{!]]):format(raw.text) | |
return { | |
marked_raw_inline('latex', text1); | |
pandoc.Code(prl_label); | |
marked_raw_inline('latex', [[!}]]); | |
} | |
end | |
end | |
local function make_subst_source(rawtexts) | |
local source = [[\PRLFinish]] | |
for i = #rawtexts, 1, -1 do | |
source = ([[\PRLSubst{%s}{%s}]]):format(source, rawtexts[i]) | |
end | |
return source | |
end | |
-- Header要素に対する処理. | |
local function convert_Header(header) | |
local rawtexts = {} | |
header = pandoc.walk_block(header, { | |
RawInline = function(raw) | |
return convert_raw_latex(raw, rawtexts) | |
end; | |
}) | |
if #rawtexts == 0 then return end | |
insert(header.content, pandoc.RawInline('latex', | |
[[}{}\PRLPdf{]] .. make_subst_source(rawtexts) | |
)) | |
return header | |
end | |
-- 文書全体に対する処理. | |
local function convert_Pandoc(doc) | |
log('convert %d elements', convert_count) | |
end | |
---------------------------------------- フェーズ'unmark' | |
local unmark_count = 0 | |
-- RawInline要素に対する処理. | |
local function unmark_RawInline(raw) | |
if is_marked_raw_latex(raw) then | |
unmark_count = unmark_count + 1 | |
unmark_raw_inline(raw) | |
return raw | |
end | |
end | |
-- 文書全体に対する処理. | |
local function unmark_Pandoc(doc) | |
log('unmark %d elements', unmark_count) | |
insert(doc.blocks, 1, pandoc.RawBlock('latex', LATEX_PROLOGUE)) | |
return doc | |
end | |
---------------------------------------- フィルタ定義 | |
return { | |
{-- フェーズconvert | |
Header = convert_Header; | |
Pandoc = convert_Pandoc; | |
}; | |
{-- フェーズunmark | |
RawInline = unmark_RawInline; | |
Pandoc = unmark_Pandoc; | |
}; | |
} | |
---------------------------------------- おしまい |
\documentclass[uplatex,dvipdfmx,a4paper,jis2004]{jsarticle} | |
\usepackage{otf} | |
\usepackage[unicode,ipaex]{pxchfon} | |
\usepackage[colorlinks]{hyperref} | |
\usepackage{pxjahyper} | |
\begin{document} | |
\input{test} | |
\end{document} |