Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams \end{document}

The idea is to execute /usr/local/bin/my-shell-script.sh at the moment of .tex document processing and inject its output into LaTeX stream. Is it possible at all?

PS. It's possible through the package I've made: iexec

Maybe you could move your possible solution with your iexec package into a new answer with a short example? This might give it more visibility than hiding it in a post scriptum not everybody reads. samcarter_is_at_topanswers.xyz Jul 10, 2021 at 11:55 @samcarter_is_at_topanswers.xyz the question is "closed," I can't post any new answers to it :( yegor256 Jul 15, 2021 at 11:13 @yegor256 Yes, I noticed that when I tried to close a new question as duplicate of this :( I already voted to reopen, but no result yet :( samcarter_is_at_topanswers.xyz Jul 15, 2021 at 11:17

I would do something like the following (partially motivated by what Roman suggested): make your LaTeX file be

\documentclass{article}
\begin{document}
\input{scriptoutput.tex}
\end{document}

and generate the file scriptoutput.tex using

/usr/local/bin/my-shell-script.sh > scriptoutput.tex

You could encode this in a makefile if you want to have it run automatically when necessary. Alternatively, you could use the TeX \write18 command,

\documentclass{article}
\immediate\write18{/usr/local/bin/my-shell-script.sh > scriptoutput.tex}
\begin{document}
\input{scriptoutput.tex}
\end{document}

and I think that would automatically run the shell script each time you compile the document. The \immediate is necessary to ensure that the script is run when LaTeX encounters the command, rather than waiting until a page of output is written. (See this question for more on the shipout routine.)

Your solution is certainly more clean than mine. Mine is handy if you have a lot of substitutions though. – Roman Cheplyaka Jul 15, 2010 at 6:46 @DavidZ You might want to use \immediate\write18 instead of bare \write18. Without \immediate the write operation might be buffered until the next time the shipout routine is called. – Henri Menke Apr 10, 2014 at 17:47

As David pointed out, you can use \write18 to call external programs, then \input the resultant output file. However you will probably want to use \immediate\write18 to make sure the script is executed before calling the \input.

Alternatively, if you use newer versions of pdf(la)tex (after 1.40, I think), you can pipe the output directly into the document, by using a piped input command:

\documentclass{article}
\begin{document}
\input{|"/usr/local/bin/my-shell-script.sh"}
\end{document}

For either method you will need to enable external program calls. For TeXlive distributions, you need to call latex with the -shell-escape option, or for MikTeX, I believe the option is -enable-write18.

If you use TeXworks on Windows, you might want to pass --tex-option=--shell-escape instead of just --shell-escape. Otherwise, you will get a cryptic error message: Sorry, but "MiKTeX Compiler Driver" did not succeed. – GuiTeK Aug 23, 2018 at 15:05 The \input pipe even works with a shell pipe. E.g., \input{|"jq -r .my_key my_file.json | wc"} works! – rickhg12hs Jun 19, 2019 at 2:05 [This section currently is \input{|"wc kmb-box.tex| tr -s ' ' | cut -d' ' -f 4"} % 2000 characters are allowed here \input{kmb-box}

works nicely. ie, this uses wordcount (wc) to report how many characters are in the file kmb-box.tex, which is part of (included in) the document.

(btw If you wanted words rather than characters, just change the number in "-f 4")

I get errors when I switch the command to "ls". Is there a problem with newlines in the output of the command? – Alec Jacobson Aug 27, 2013 at 9:53

Unless it is imperative that the script is run while LaTeX is running I would recommend just using make to run LaTeX and you script.

I have used that approach to add word counting for articles and including statistics on bibliographic references.

Let your script generate a .tex file and include that in you LaTeX source file.

Below is a snippet from one of my Makefiles:

TEX =   /usr/texbin/pdflatex
PREVIEW = /usr/bin/open
REPORT  =   SimMon
REPORT_MASTER = $(REPORT).tex
TEX_OPTIONS = -halt-on-error
SimMon:  $(REPORT_MASTER) countRefferedPages
    $(TEX) $(TEX_OPTIONS) $(REPORT_MASTER)
    @$(PREVIEW) $(REPORT).pdf
countRefferedPages: BibTeXPageCount
    cat *.tex | support/BPC/build/Debug/BPC Castle.bib > litteraturTotal.tex
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.