我试图在TextMate中输入一些UTF-8字符到LaTeX文件(它说它的默认编码是UTF-8),但LaTeX似乎不理解它们。
运行cat my_file.tex可以在Terminal中正确显示字符。运行ls -al会显示一些我以前从未见过的东西:文件列表旁边的“@”:
-rw-r--r--@ 1 me users 2021 Feb 11 18:05 my_file.tex
(并且,是的,我在LaTeX中使用\usepackage[utf8]{inputenc}。)
我找到了iconv,但这似乎不能告诉我编码是什么-它只会转换一旦我弄清楚。
我实现了下面的bash脚本,它为我工作。
它首先尝试将file——mime-encoding返回的encoding中的iconv转换为utf-8。
如果失败,它将遍历所有编码,并显示原始文件和重新编码的文件之间的差异。它跳过了产生较大diff输出的编码(“大”由MAX_DIFF_LINES变量或第二个输入参数定义),因为这些编码很可能是错误的。
如果使用这个脚本导致了“不好的事情”,不要责怪我。这里有一个rm -f,所以有怪物。我试图通过对带有随机后缀的文件使用它来防止不良影响,但我不做任何承诺。
在Darwin 15.6.0上测试。
#!/bin/bash
if [[ $# -lt 1 ]]
then
echo "ERROR: need one input argument: file of which the enconding is to be detected."
exit 3
fi
if [ ! -e "$1" ]
then
echo "ERROR: cannot find file '$1'"
exit 3
fi
if [[ $# -ge 2 ]]
then
MAX_DIFF_LINES=$2
else
MAX_DIFF_LINES=10
fi
#try the easy way
ENCOD=$(file --mime-encoding $1 | awk '{print $2}')
#check if this enconding is valid
iconv -f $ENCOD -t utf-8 $1 &> /dev/null
if [ $? -eq 0 ]
then
echo $ENCOD
exit 0
fi
#hard way, need the user to visually check the difference between the original and re-encoded files
for i in $(iconv -l | awk '{print $1}')
do
SINK=$1.$i.$RANDOM
iconv -f $i -t utf-8 $1 2> /dev/null > $SINK
if [ $? -eq 0 ]
then
DIFF=$(diff $1 $SINK)
if [ ! -z "$DIFF" ] && [ $(echo "$DIFF" | wc -l) -le $MAX_DIFF_LINES ]
then
echo "===== $i ====="
echo "$DIFF"
echo "Does that make sense [N/y]"
read $ANSWER
if [ "$ANSWER" == "y" ] || [ "$ANSWER" == "Y" ]
then
echo $i
exit 0
fi
fi
fi
#clean up re-encoded file
rm -f $SINK
done
echo "None of the encondings worked. You're stuck."
exit 3