【问题标题】:Compiling Latex from Linux bash script从 Linux bash 脚本编译 Latex
【发布时间】:2017-09-28 13:11:43
【问题描述】:

我正在使用以下脚本在 Linux 的命令行上将 Latex 文档编译为 pdf:

#! /bin/bash
NAME=`echo "$1" | cut -d'.' -f1`
pdflatex -file-line-error -halt-on-error $NAME.tex
xdg-open $NAME.pdf

它可以工作,但即使 pdflatex 编译出现错误,xdg-open 行也会运行并显示任何以前创建的 pdf 文件。

如何为最后一行代码添加条件语句,即 xdg-open 只有在 pdflatex 的上一行编译成功时才应该运行?否则,它应该给出错误消息,而不是尝试显示任何 pdf 文件。 pdflatex 是否返回任何可以在 bash 脚本中检查的错误代码?感谢您的帮助。

【问题讨论】:

    标签: linux bash compiler-errors latex


    【解决方案1】:
    #! /bin/bash
    NAME=`echo "$1" | cut -d'.' -f1`
    pdflatex -file-line-error -halt-on-error $NAME.tex && xdg-open $NAME.pdf
    

    应该做的伎俩。

    编辑以解决评论中的问题:

    当然,这可以将其他命令包装在子外壳中:

    pdflatex -file-line-error -halt-on-error $NAME.tex && ( xdg-open $NAME.pdf; command2; command 3 )
    

    或者通过在 if 语句中计算返回码:

    if [[ $? -eq 0 ]]; then
      xdg-open $NAME.pdf
      command2
      command3
    fi
    

    【讨论】:

    • 如果出现错误还需要添加其他语句怎么办?我可以做一个 if-then-else 块吗?
    • 完美。两种我不知道的新方法。
    • 酷。很高兴有帮助。 =}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-25
    • 1970-01-01
    • 2011-04-09
    • 1970-01-01
    • 2019-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多