I want to add a bash command that does the following:
Currently, I need to run this:
$ pdflatex <filename>.tex | open <filename>.pdf
Is there any way to convert that into something like
$ complatex <filename>
and have both commands executed?
I want to add a bash command that does the following:
Currently, I need to run this:
$ pdflatex <filename>.tex | open <filename>.pdf
Is there any way to convert that into something like
$ complatex <filename>
and have both commands executed?
How about this for a start:
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
}
Oneline version:
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 ;}
This function tests for an argument, if there is one it just runs pdflatex saving the output files in the argument's directory (instead of the current one) and opens the output .pdf in the default PDF viewer. If you call it without an argument, it goes through every .tex file in the current directory, tests whether there's a .pdf with the same name and only if not does the same as above.
To make the complatex command available on your system, just copy one of the two versions from above into either your ~/.bash_aliases (create it if necessary) or your ~/.bashrc file and open up a new terminal or source the changed file in an existing one with e.g. 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
{}a syntax infindcommand? – mja May 06 '18 at 01:12pdflatexdoesn't usually write to stdout (b) at least on Linux systemsopendoesn't usually open files and (c) even if it did, there would be no reason to pipe to its stdin; I'd expect something more likepdflatex somefile.tex ; xdg-open somefile.pdf– steeldriver May 06 '18 at 01:21help function. – Cyrus May 06 '18 at 07:04- I used {} as a placeholder for a name. Nothing fancy.
- To answer the other question,
– Landmaster May 06 '18 at 16:02pdflatexdoesn't write to stdout, but it will compile the pdf into the directory, so then, right after I compile it, I want to grab that compiled pdf and display it on my computer.openopens files in macos. The actual system I use to open a file is outside the premise of this question, so don't worry about that :)user$ pdflatex main.tex | open main.pdfworks perfectly fine in my computer :D