Skip to content

Instantly share code, notes, and snippets.

@ultimate010
Created December 25, 2015 11:05
Show Gist options
  • Save ultimate010/d322f34d7efed5b8739d to your computer and use it in GitHub Desktop.
Save ultimate010/d322f34d7efed5b8739d to your computer and use it in GitHub Desktop.
Use xelatex to generate pdf from src
#!/usr/bin/env bash
[[ debug -eq 1 ]] && set -x
PROGNAME=$(basename $0)
error_exit()
{
# ----------------------------------------------------------------
# Function for exit due to fatal program error
# Accepts 1 argument:
# string containing descriptive error message
# ----------------------------------------------------------------
echo "${PROGNAME}: ${1:-"Unknown Error"}" 1>&2
exit 1
}
clean_up() {
# Perform program exit housekeeping
# Optionally accepts an exit status
rm -rf ./_minted*
bname=$(basename `pwd`)
rm -r ${bname}.aux ${bname}.log ${bname}.out ${bname}.toc ${bname}.tex
echo "Clean tmp file"
exit $1
}
trap clean_up 0 SIGHUP SIGINT SIGTERM
tex_file=$(basename `pwd`).tex #mktemp ## Random temp file name
langue=$1
if [ $# -lt 1 ]; then
echo "Please input ./$PROGNAME python|matlab|cpp|c"
exit 0
fi
cat<<EOF >$tex_file ## Print the tex file header
\documentclass[a4paper]{article}
%%%%%%%%%% 字体支持 %%%%%%%%%%%%
\usepackage[slantfont,boldfont]{xeCJK} % 允许斜体和粗体
% \setCJKmainfont{simsun.ttf} % 设置缺省中文字体
% \setCJKsansfont{simhei.ttf} % 设置无衬线字体
% \setCJKmonofont{simfang.ttf} % 设置等宽字体
\setmonofont{Monaco} % 英文等宽字体
\setsansfont{Trebuchet MS} % 英文无衬线字体
\usepackage[top=1in, bottom=1in, left=1in, right=1in]{geometry}
%%%%%%%%%%%%%%%%%%%%% Begin fix contents table num to wide %%%%%%%%%%%%%%%%%%%%%
\documentclass[pagesize]{scrreprt}
\usepackage[tocindentauto]{tocstyle}
\usetocstyle{KOMAlike} %the previous line resets it
%%%%%%%%%%%%%%%%%%%%% End fix contents table num to wide %%%%%%%%%%%%%%%%%%%%%
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage[]{minted}
\usepackage{tcolorbox}
\usepackage{hyperref}
\hypersetup{
colorlinks,
% citecolor=black,
% filecolor=black,
% linkcolor=black,
% urlcolor=black
}
\usemintedstyle{emacs}
\title{}
\author{Print By Zaks}
\date{\today}
\begin{document}
\tableofcontents % 插入目录
\thispagestyle{empty} % 首页无页眉页脚
\clearpage
EOF
case $langue in
python ) echo "Process python code"
files=$(find . -type f -name "*.py" ! -regex ".*/\..*" ! -name ".*" ! -name "*~" ! -name 'src2pdf' | sed 's/^\..//')
;;
matlab ) echo "Process matlab code"
files=$(find . -type f -name "*.m" ! -regex ".*/\..*" ! -name ".*" ! -name "*~" ! -name 'src2pdf' | sed 's/^\..//')
;;
cpp ) echo "Process cplusplus code"
files=$(find . -type f -name "*.cpp" -o -name "*.h" -o -name "*.hpp" -o -name "*.c" -o -name "*.cc" ! -regex ".*/\..*" ! -name ".*" ! -name "*~" ! -name 'src2pdf' | sed 's/^\..//')
langue='c++'
;;
c ) echo "Process c code"
files=$(find . -type f -name "*.cpp" -o -name "*.h" -o -name "*.hpp" -o -name "*.c" -o -name "*.cc" ! -regex ".*/\..*" ! -name ".*" ! -name "*~" ! -name 'src2pdf' | sed 's/^\..//')
;;
* ) error_exit "Please input python|matlab|cpp|c"
;;
esac
for filename in $files; do ## Loop through each file
encode=$(file $filename | awk '{print $2}')
if [ $encode == 'ISO-8859' ]; then
encode='gbk'
fi
name=$(echo "$filename" | sed 's/_/\\_/g')
echo "\\section{$name}" >> $tex_file ## Create a section for each file
echo '\begin{minted}' >> $tex_file
echo '[' >> $tex_file
echo 'frame=single, framesep=10pt, ' >> $tex_file
echo 'breaklines, breakanywhere, fontfamily=courier, ' >> $tex_file
echo 'mathescape=true, ' >> $tex_file
echo 'autogobble=true, ' >> $tex_file
echo 'fontsize=\footnotesize, ' >> $tex_file
echo 'linenos ' >> $tex_file
echo ']' >> $tex_file
echo "{$langue}" >> $tex_file
if [ $encode == 'gbk' ]; then
echo "Trans encode for $filename"
cat $filename | iconv -f gb18030 -t utf-8 | perl -pe "s/\r\n$/\n/" | perl -pe "s/\t/ /g" | cat >> $tex_file
else
cat $filename | perl -pe "s/\r\n$/\n/" | perl -pe "s/\t/ /g" | cat >> $tex_file
fi
echo '\end{minted}' >> $tex_file
# echo '\clearpage' >> $tex_file
done &&
echo ' \end{document}' >> $tex_file &&
xelatex -interaction=nonstopmode --shell-escape $tex_file -output-directory .
sleep 1
xelatex -interaction=nonstopmode --shell-escape $tex_file -output-directory .
echo "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment