-
Notifications
You must be signed in to change notification settings - Fork 4
/
platex2pdf.sh
executable file
·72 lines (63 loc) · 1.97 KB
/
platex2pdf.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/bin/sh
########################################################################
# platex2pdf.sh: LaTeX to PDF Conversion Script
#
# Description:
# This script converts LaTeX (.tex) files to PDF, handling various
# encodings and using platex or uplatex depending on the document class.
# It requires platex, uplatex, dvipdfmx, and nkf to be installed.
#
# Author: id774 (More info: http://id774.net)
# Source Code: https://github.com/id774/scripts
# License: LGPLv3 (Details: https://www.gnu.org/licenses/lgpl-3.0.html)
# Contact: [email protected]
#
# Version History:
# v1.2 2024-01-23
# Integrated check_commands function for dependency checks.
# v1.1 2023-12-05
# Refactored for improved readability, added environment checks.
# v1.0 2014-05-21
# Initial release. Converts LaTeX files to PDF.
#
# Usage:
# ./platex2pdf.sh [tex-file]
#
########################################################################
check_commands() {
for cmd in "$@"; do
if ! command -v "$cmd" >/dev/null 2>&1; then
echo "Error: Command '$cmd' is not installed. Please install $cmd and try again."
exit 127
elif ! [ -x "$(command -v "$cmd")" ]; then
echo "Error: Command '$cmd' is not executable. Please check the permissions."
exit 126
fi
done
}
# Check for required commands
check_commands platex uplatex dvipdfmx nkf
# Check for input file
if [ -z "$1" ]; then
echo "Usage: $0 [tex-file]"
exit 1
fi
TEX=$*
DVI=`/usr/bin/basename "$TEX" ".tex"`
# Detect encoding
THECODE=`nkf -g "$TEX"`
case $THECODE in
UTF-8) KANJI="-kanji=utf8";;
EUC-JP) KANJI="-kanji=euc";;
Shift-JIS) KANJI="kanji=sjis";;
ISO-2022-JP) KANJI="-kanji=jis";;
esac
# Determine LaTeX command to use
PLATEX="platex"
CLASS=`sed -n '/documentclass/p' "$TEX" | sed '/%.*documentclass/d' | sed -n '1p'`
case $CLASS in
*{u*) PLATEX="uplatex";;
esac
# Execute LaTeX and PDF conversion
$PLATEX $KANJI "$TEX"
dvipdfmx "$DVI"