Joh*_*ane 14
使用最新版本的pandoc(1.12.2),您可以执行以下操作:
pandoc -f html+tex_math_dollars+tex_math_single_backslash -t latex
Run Code Online (Sandbox Code Playgroud)
好多了!如果你不想转换由\(和分隔的数学,那就\)做
pandoc -f html+tex_math_dollars -t latex
Run Code Online (Sandbox Code Playgroud)
这不是一件容易的事.这是一个应该有效的解决方案,前提是您只使用$和$$作为数学分隔符,并假设您的文档不包含任何其他用途$.(如果你不能假设,你可以尝试在下面的内容中调整perl正则表达式.)
步骤1:安装Haskell平台(如果您还没有),并安装'cabal install pandoc'以获取pandoc库.(如果您使用二进制安装程序安装了pandoc,则只有可执行文件,而不是Haskell库.)
第2步:现在编写一个小的Haskell脚本 - 我们称之为fixmath.hs:
import Text.Pandoc
main = toJsonFilter fixmath
fixmath :: Block -> Block
fixmath = bottomUp fixmathBlock . bottomUp fixmathInline
fixmathInline :: Inline -> Inline
fixmathInline (RawInline "html" ('<':'!':'-':'-':'M':'A':'T':'H':xs)) =
RawInline "tex" $ take (length xs - 3) xs
fixmathInline x = x
fixmathBlock :: Block -> Block
fixmathBlock (RawBlock "html" ('<':'!':'-':'-':'M':'A':'T':'H':xs)) =
RawBlock "tex" $ take (length xs - 3) xs
fixmathBlock x = x
Run Code Online (Sandbox Code Playgroud)
编译:
ghc --make fixmath.hs
Run Code Online (Sandbox Code Playgroud)
这将为您提供可执行文件fixmath.现在,假设您的输入文件是input.html,以下命令应该将其转换为乳胶并且数学完整,将结果放入output.html:
cat input.html | \
perl -0pe 's/(\$\$?[^\$]+\$\$?)/\<!--MATH$1-->/gm' | \
pandoc -s --parse-raw -f html -t json | \
./fixmath | \
pandoc -f json -t latex -s > output.tex
Run Code Online (Sandbox Code Playgroud)
第一部分是perl one-liner,它将您的数学位置放在标记为"MATH"的特殊HTML注释中.第二部分将HTML解析为与文档对应的Pandoc数据结构的JSON表示.然后fixmath转换此结构,将特殊HTML注释更改为原始LaTeX块和内联.(有关解释,请参阅使用pandoc编写脚本.)最后,我们将JSON转换回LaTeX.