问题描述
我将pandoc用作库,相关的代码段为:
module Lib
( latexDirToTex,latexToTxt
) where
import qualified Data.ByteString as BS
import Data.List (isSuffixOf)
import qualified Data.Text as T
import qualified Data.Text.IO as TIO
import ForeignLib (chdir)
import Path
import System.Directory (getDirectoryContents )
import Text.Pandoc
import Text.Pandoc.UTF8 (toText)
latexToTxt :: Path b File -> IO T.Text
latexToTxt fPath = do
fileBS <- BS.readFile $ toFilePath fPath
result <- runIO $ do
doc <- readLaTeX def $ toText fileBS
writePlain def doc
handleError result
由此可见,我基本上只是在调用readLaTeX
来读取LaTeX文档。
但是,当我尝试运行此代码时,我在实践中遇到了很多麻烦,出现了标题中的错误:
[WARNING] Could not convert TeX math '\begin{array}{ccccccccccc}
& & 1 & 2 & 4 & 7 & 11 & 15 & 15 & & \\
\hline
0 & \vline & 1 & 0 & 0 & 0 & 0 & 0 & 0 & \vline & 1 \\
1 & \vline & 1 & 1 & 0 & 0 & 0 & 0 & 0 & \vline & 3 \\
2 & \vline & 1 & 2 & 1 & 0 & 0 & 0 & 0 & \vline & 9 \\
3 & \vline & 1 & 3 & 3 & 1 & 0 & 0 & 0 & \vline & 26 \\
4 & \vline & 1 & 4 & 6 & 4 & 1 & 0 & 0 & \vline & 72 \\
5 & \vline & 1 & 5 & 10 & 10 & 5 & 1 & 0 & \vline & 191 \\
6 & \vline & 0 & 6 & 15 & 20 & 15 & 6 & 1 & \vline & 482 \\
7 & \vline & 0 & 0 & 21 & 35 & 35 & 21 & 7 & \vline & 1134 \\
8 & \vline & 0 & 0 & 0 & 56 & 70 & 56 & 28 & \vline & 2422 \\
9 & \vline & 0 & 0 & 0 & 0 & 126 & 126 & 34 & \vline & 4536 \\
10 & \vline & 0 & 0 & 0 & 0 & 0 & 252 & 210 & \vline & 6930 \\
11 & \vline & 0 & 0 & 0 & 0 & 0 & 0 & 462 & \vline & 6930
\end{array}',rendering as TeX:
0 & \vline & 1 & 0 & 0 & 0 & 0 & 0 &
^
unexpected "\\"
expecting "&","\\\\",white space or "\\end"
arxiv-pandoc-static: <stdout>: commitAndReleaseBuffer: invalid argument (invalid character)
与直接使用pandoc可执行文件相比,没有发生此类错误,并且我收到了很好的输出。我想将pandoc阅读器配置为尽可能灵活,并且不对错误进行救助(或者更好的是,首先避免错误)。如何通过pandoc API实现此目标?
解决方法
我认为这不是pandoc问题,而是GHC或 text 软件包中的一个问题。答案可以在完全不相关的Haskell项目hledger docs中找到:
遇到诸如“非法字节序列”或“无效或 不完整的多字节或宽字符”或“ commitAndReleaseBuffer: 无效的参数(无效的字符)”
使用GHC编译的程序(hledger,haskell构建工具等)需要 在环境中配置了可识别UTF-8的语言环境,否则它们 遇到非ascii时,将因这些错误而失败 字符。
要修复此问题,请将LANG环境变量设置为某些语言环境, 支持UTF-8。您选择的语言环境必须安装在系统上。
因此在您的shell中运行类似export LANG=C.UTF-8
之类的东西应该可以解决此问题。