我想添加一个执行以下操作的 bash 命令:
目前,我需要运行这个:
$ pdflatex <filename>.tex | open <filename>.pdf
有什么方法可以把它转换成类似的东西吗
$ complatex <filename>
并且两个命令都执行了吗?
答案1
首先这样:
complatex ()
{
if [[ -n $1 ]]; then
pdflatex -output-directory $(dirname "$1") "$1" &&
xdg-open "${1%%.tex}.pdf"
else
for i in *.tex; do
if [[ ! -f ${i%%.tex}.pdf ]]; then
pdflatex "$i" &&
xdg-open "${i%%.tex}.pdf"
fi
done
fi
}
单线版本:
complatex(){ if [[ $1 ]]; then pdflatex -output-directory $(dirname "$1") "$1" && xdg-open "${1%%.tex}.pdf"; else for i in *.tex; do if [[ ! -f ${i%%.tex}.pdf ]]; then pdflatex "$i" && xdg-open "${i%%.tex}.pdf"; fi; done; fi ;}
此函数测试参数,如果有参数,则它只会运行pdflatex
并将输出文件保存在参数的目录中(而不是当前目录),并.pdf
在默认 PDF 查看器中打开输出。如果您在不带参数的情况下调用它,它会遍历.tex
当前目录中的每个文件,测试是否存在.pdf
同名文件,并且只有当不存在时才执行与上述相同的操作。
为了使该complatex
命令在您的系统上可用,只需将上述两个版本之一复制到您的~/.bash_aliases
(如果需要,请创建它)或您的~/.bashrc
文件中,然后打开一个新终端或在现有终端中使用例如来获取更改的文件source ~/.bash_aliases
。
示例运行
$ tree -A --noreport
.
├── dummy.pdf
├── dummy.tex
├── other\ dir
└── test.tex
$ complatex test.tex &>/dev/null # opens test.pdf in PDF viewer
$ tree -A --noreport
.
├── dummy.pdf
├── dummy.tex
├── other\ dir
├── test.aux
├── test.log
├── test.pdf
└── test.tex
$ rm -f test.!(tex) # removes the output files that were just created
$ cd other\ dir/
$ complatex ../test.tex &>/dev/null # opens test.pdf in PDF viewer
$ ls # other dir stays empty
$ cd ..
$ tree -A --noreport
.
├── dummy.pdf
├── dummy.tex
├── other\ dir
├── test.aux
├── test.log
├── test.pdf
└── test.tex
$ rm -f test.!(tex) # removes the output files that were just created
$ complatex &>/dev/null # opens test.pdf in PDF viewer, doesn't process dummy.tex as there's a dummy.pdf already
$ tree -A --noreport
.
├── dummy.pdf
├── dummy.tex
├── other\ dir
├── test.aux
├── test.log
├── test.pdf
└── test.tex