LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 3764|回复: 5

latex2pdf的脚本

[复制链接]
发表于 2003-10-3 11:55:27 | 显示全部楼层 |阅读模式
由于本人学LaTeX也不久,所以有些功能可能没想到,欢迎大家对程序修改,适应各自的需要。本脚本已可以处理索引了,以后将会不断更新。


  1. #!/bin/bash
  2. #
  3. #   "latex2pdf.sh" is a script written in bash to make processing latex
  4. #   file easier.
  5. #   Copyright (C) 2003 alphatan<alphatan@263.net>  version 0.51
  6. #
  7. #   This program is free software; you can redistribute it and/or modify
  8. #   it under the terms of the GNU General Public License as published by
  9. #   the Free Software Foundation; either version 2 of the License, or
  10. #   (at your option) any later version.
  11. #
  12. #    This program is distributed in the hope that it will be useful,
  13. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. #    GNU General Public License for more details.
  16. #
  17. #    You should have received a copy of the GNU General Public License
  18. #    along with this program; if not, write to the Free Software
  19. #    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. #


  21. target=${1%.tex}
  22. action=$2

  23. declare -f dvi errorMsg ldvi lpdf lpdfmx lps lview pdf pdfmx ps view

  24. function errorMsg()
  25. {
  26.         echo -e "    Usage: ${0##*/} <filename> [action] " >&2
  27.         echo -e "         <filename> : the tex file with '.tex' extension. "
  28.         echo -e "                          This could not be ignored."
  29.         echo -e "         [action]   : the 'action' you wish to operate on the '.tex' file"
  30.         echo -e "             dvi   : process to 'dvi' output"
  31.         echo -e "             ps    : process to 'ps' output"
  32.         echo -e "             pdf   : process to 'pdf' output with 'dvipdf' command "
  33.         echo -e "             pdfmx : process to 'pdf' output with 'dvipdfmx' command "
  34.         echo -e "             view  : view the '<filename>.pdf' with 'acroread' \n"
  35.         echo -e "             The action type above could put an 'l' precede it. eg. 'ldvi' "
  36.         echo -e "        it will call the corresponding process and do a cleaning course by "
  37.         echo -e "        the end. "
  38.         echo -e "             If the 'action' is ignored, it is refer to 'lview', it would "
  39.         echo -e "        cause 'lpdfmx' and 'cleaning course', and view the '.pdf' output "
  40.         echo -e "        with 'acroread'"
  41. }

  42. if [ ! -e ${target}.tex ]; then
  43.         echo -e "ERROR!!: ${target}.tex does not exist! " >&2
  44.         errorMsg
  45.         exit 1
  46. fi


  47. function dvi()
  48. {
  49.         if latex ${target}.tex ; then
  50.                 if [ -e ${target}.idx ] ; then
  51.                         if makeindex ${target}.idx ; then
  52.                                 if latex ${target}.tex ; then
  53.                                         return 0;
  54.                                 else return 1
  55.                                 fi
  56.                         else return 1
  57.                         fi
  58.                 else
  59.                         return 0
  60.                 fi
  61.         else return 1
  62.         fi
  63. }

  64. function ps()
  65. {
  66.         if dvi ; then
  67.                 if dvips ${target}.dvi ; then
  68.                         return 0;
  69.                 else return 1
  70.                 fi
  71.         else return 1
  72.         fi
  73. }

  74. function pdf()
  75. {
  76.         if dvi ; then
  77.                 if dvipdf ${target}.dvi ; then
  78.                         return 0;
  79.                 else return 1
  80.                 fi
  81.         else return 1
  82.         fi
  83. }

  84. function pdfmx()
  85. {
  86.         if dvi ; then
  87.                 if dvipdfmx ${target}.dvi ; then
  88.                         return 0;
  89.                 else return 1
  90.                 fi
  91.         else return 1
  92.         fi
  93. }

  94. function view()
  95. {
  96.         if [ -e ${target}.pdf ] ; then
  97.                 acroread ${target}.pdf
  98.         else return 1
  99.         fi
  100. }

  101. function cleanFiles()
  102. {
  103.         echo "Cleaning process caught ... "
  104.         unset i;
  105.         for i in aux dvi idx ilg ind lof log lot pdf ps ; do
  106.                 if [ ! $i = $1 ]; then
  107.                         if [ -e ${target}.$i ]; then
  108.                                 rm -f ${target}.$i
  109.                         fi
  110.                 fi
  111.         done
  112.         unset i;
  113. }

  114. function ldvi()
  115. {
  116.         if dvi ; then
  117.                 cleanFiles dvi
  118.         else return 1
  119.         fi
  120. }

  121. function lps()
  122. {
  123.         if ps ; then
  124.                 cleanFiles ps
  125.         else return 1
  126.         fi
  127. }

  128. function lpdf()
  129. {
  130.         if pdf ; then
  131.                 cleanFiles pdf
  132.         else return 1
  133.         fi
  134. }

  135. function lpdfmx()
  136. {
  137.         if pdfmx ; then
  138.                 cleanFiles pdf
  139.         else return 1
  140.         fi
  141. }

  142. function lview()
  143. {
  144.         if lpdfmx ; then
  145.                 if acroread ${target}.pdf ; then
  146.                         cleanFiles pdf
  147.                         return 0
  148.                 else return 1
  149.                 fi
  150.         else return 1
  151.         fi
  152. }

  153. case $action in
  154.         dvi ) eval $action ;;
  155.         ps )  eval $action ;;
  156.         pdf ) eval $action ;;
  157.         pdfmx ) eval $action ;;
  158.         view ) eval $action ;;
  159.         ldvi ) eval $action ;;
  160.         lps ) eval $action ;;
  161.         lpdfmx ) eval $action ;;
  162.         lview ) eval $action ;;
  163.         '' ) eval lpdfmx ;;
  164.         * ) echo -e "Error Action type! " >&2
  165.                 errorMsg
  166.                 exit 1 ;;
  167. esac
复制代码
发表于 2003-10-3 18:12:22 | 显示全部楼层
不错的脚本,我已经收藏到[脚本欣赏区]里啦~~谢谢:thank
 楼主| 发表于 2003-10-5 02:56:25 | 显示全部楼层
居然贴子有修改期限……
这里只好再发,因为这个脚本功能比较单独,所以以后的更新就不再加贴了。欲获得最新版可以到水木清华的TeX版或是到我的ftp的personalWorks/alphatan/latex2pdf/下载(可能经常上不了……)。

0.55
修改了dvi函数,使其可以处理cross-reference。
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;一如既往地欢迎任何意见。

  1. #!/bin/bash
  2. #
  3. #   "latex2pdf.sh" is a script written in bash to make processing latex
  4. #   file easier.
  5. #   Copyright (C) 2003 alphatan<alphatan@263.net>  version 0.55
  6. #
  7. #   This program is free software; you can redistribute it and/or modify
  8. #   it under the terms of the GNU General Public License as published by
  9. #   the Free Software Foundation; either version 2 of the License, or
  10. #   (at your option) any later version.
  11. #
  12. #    This program is distributed in the hope that it will be useful,
  13. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. #    GNU General Public License for more details.
  16. #
  17. #    You should have received a copy of the GNU General Public License
  18. #    along with this program; if not, write to the Free Software
  19. #    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. #


  21. target=${1%.tex}
  22. action=$2

  23. declare -f dvi errorMsg ldvi lpdf lpdfmx lps lview pdf pdfmx ps view

  24. function errorMsg()
  25. {
  26.     echo -e "    Usage: ${0##*/} <filename> [action] " >&2
  27.     echo -e "         <filename> : the tex file with '.tex' extension. "
  28.     echo -e "                          This could not be ignored."
  29.     echo -e "         [action]   : the 'action' you wish to operate on the '.tex' file"
  30.     echo -e "             dvi   : process to 'dvi' output"
  31.     echo -e "             ps    : process to 'ps' output"
  32.     echo -e "             pdf   : process to 'pdf' output with 'dvipdf' command "
  33.     echo -e "             pdfmx : process to 'pdf' output with 'dvipdfmx' command "
  34.     echo -e "             view  : view the '<filename>.pdf' with 'acroread' \n"
  35.     echo -e "             The action type above could put an 'l' precede it. eg. 'ldvi' "
  36.     echo -e "        it will call the corresponding process and do a cleaning course by "
  37.     echo -e "        the end. "
  38.     echo -e "             If the 'action' is ignored, it is refer to 'lview', it would "
  39.     echo -e "        cause 'lpdfmx' and 'cleaning course', and view the '.pdf' output "
  40.     echo -e "        with 'acroread'"
  41. }

  42. if [ ! -e ${target}.tex ]; then
  43.         echo -e "ERROR!!: ${target}.tex does not exist! " >&2
  44.         errorMsg
  45.         exit 1
  46. fi


  47. function dvi()
  48. {
  49.         if latex ${target}.tex ; then
  50.                 if [ -e ${target}.idx ] ; then
  51.                         if makeindex ${target}.idx ; then
  52.                                 if latex ${target}.tex ; then
  53.                                         return 0;
  54.                                 else return 1
  55.                                 fi
  56.                         else return 1
  57.                         fi
  58.                 elif grep -in 'LaTeX Warning: Label(s) may have changed. Rerun to get cross-references right.' ${target}.log ; then
  59.                         if latex ${target}.tex ; then
  60.                                 return 0
  61.                 fi
  62.         else return 1
  63.         fi
  64. }

  65. function ps()
  66. {
  67.         if dvi ; then
  68.                 if dvips ${target}.dvi ; then
  69.                         return 0;
  70.                 else return 1
  71.                 fi
  72.         else return 1
  73.         fi
  74. }

  75. function pdf()
  76. {
  77.         if dvi ; then
  78.                 if dvipdf ${target}.dvi ; then
  79.                         return 0;
  80.                 else return 1
  81.                 fi
  82.         else return 1
  83.         fi
  84. }

  85. function pdfmx()
  86. {
  87.         if dvi ; then
  88.                 if dvipdfmx ${target}.dvi ; then
  89.                         return 0;
  90.                 else return 1
  91.                 fi
  92.         else return 1
  93.         fi
  94. }

  95. function view()
  96. {
  97.         if [ -e ${target}.pdf ] ; then
  98.                 acroread ${target}.pdf
  99.         else return 1
  100.         fi
  101. }

  102. function cleanFiles()
  103. {
  104.         echo "Cleaning process caught ... "
  105.         unset i;
  106.         for i in aux dvi idx ilg ind lof log lot pdf ps ; do
  107.                 if [ ! $i = $1 ]; then
  108.                         if [ -e ${target}.$i ]; then
  109.                                 rm -f ${target}.$i
  110.                         fi
  111.                 fi
  112.         done
  113.         unset i;
  114. }

  115. function ldvi()
  116. {
  117.         if dvi ; then
  118.                 cleanFiles dvi
  119.         else return 1
  120.         fi
  121. }

  122. function lps()
  123. {
  124.         if ps ; then
  125.                 cleanFiles ps
  126.         else return 1
  127.         fi
  128. }

  129. function lpdf()
  130. {
  131.         if pdf ; then
  132.                 cleanFiles pdf
  133.         else return 1
  134.         fi
  135. }

  136. function lpdfmx()
  137. {
  138.         if pdfmx ; then
  139.                 cleanFiles pdf
  140.         else return 1
  141.         fi
  142. }

  143. function lview()
  144. {
  145.         if lpdfmx ; then
  146.                 if acroread ${target}.pdf ; then
  147.                         return 0
  148.                 else return 1
  149.                 fi
  150.         else return 1
  151.         fi
  152. }

  153. case $action in
  154.         dvi ) eval $action ;;
  155.         ps )  eval $action ;;
  156.         pdf ) eval $action ;;
  157.         pdfmx ) eval $action ;;
  158.         view ) eval $action ;;
  159.         ldvi ) eval $action ;;
  160.         lps ) eval $action ;;
  161.         lpdfmx ) eval $action ;;
  162.         lview ) eval $action ;;
  163.         '' ) eval lpdfmx ;;
  164.         * ) echo -e "Error Action type! " >&2
  165.                 errorMsg
  166.                 exit 1 ;;
  167. esac
复制代码


另外,对于本论坛的版面设置提点意见:
希望在“回复”的时候,不要把“清空内容”的按钮放在右下,我已经很多次把东西写完了却按了这东西,欲哭无泪,建议把它放到“标题”行的最后一列。
发表于 2003-10-5 17:51:49 | 显示全部楼层
用dvipdfm多好?
 楼主| 发表于 2003-10-7 02:31:03 | 显示全部楼层
你说dvipdfm是在windows用的吗?对应于linux下的dvipdfmx?
如果是的话那它是个程序而已,只是管理其中一个步骤,我写这个脚本的原因是要从.tex文件到dvi ps pdf或是在输出pdf后以acroreader来查看。
不同的东西。
不过也谢谢你的意见。
最初由 Sandy 发表
用dvipdfm多好?



另外,加个latex2pdf的history. 需要源程序的话下载水木TeX版的4231贴子。
0.57
修改了运行过latex(运行正常)但没有输出的情况。
也是利用grep 在target.log文件中找‘No pages of output.'
增加了checkDVIoutput()函数,因为没有试过成功生成了.dvi却没有生成.ps,
.pdf的情况(所以不知道此情况出现时的特征),所以只在dvi()中被调用。
另外也对grep的调用去掉了-in选项而采用-q,避免输出骚扰信息。

0.56
可以处理需要多次用latex处理的文件(我暂时还没有遇到这种情况)。
增加了multiTimeLatex()函数,其在dvi()中被调用。
                                                                                
0.55
可以处理cross-reference了。处理了dvi()函数,用grep在target.log文件中找
‘LaTeX Warning: Label(s) may have changed. Rerun to get
cross-references right.’


0.52
由于本人学LaTeX也不久,所以有些功能可能没想到,欢迎大家对程序修改,适应
各自的需要。本脚本已可以处理索引了,以后将会不断更新。
发表于 2003-12-20 22:16:09 | 显示全部楼层
FT... dvipdfmx是dvipdfm的CJK-Extension
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表