7

Say I want to call some external program using \write18, which returns some text to the command line. Can I get that text from inside TeX?

As an example

> inkscape -V

returns the version of the inkscape installation, in my case

Inkscape 0.92.4 (5da689c313, 2019-01-14)

How can I retrieve this information using TeX?

(Note: I am running TeX on Windows, but would like the solution to be system independent.)

schtandard
  • 14,892

1 Answers1

7

You could use \input|"inkscape -V" (requires -shell-escape). However, I suggest defining a macro so that you can also manipulate the output:

\documentclass{article}
\usepackage{catchfile}

\CatchFileDef{\inkscapebanner}{|"inkscape -V"}{}

\begin{document}

\inkscapebanner

\end{document}

enter image description here

An example of manipulation:

\documentclass{article}
\usepackage{catchfile}

\CatchFileDef{\inkscapebanner}{|"inkscape -V"}{}
\makeatletter
\def\@getinkscapeversioninfo#1 #2 #3, #4\@nil{%
  \def\inkscapeversion{#2}%
  \def\inkscapebuild{#3}%
  \def\inkscaperelease{#4}%
}
\expandafter\@getinkscapeversioninfo\inkscapebanner\@nil
\makeatother

\begin{document}

\inkscapebanner\par
\inkscapeversion\par
\inkscapebuild\par
\inkscaperelease\par

\end{document}

enter image description here

The mandatory expl3 version:

\documentclass{article}
\usepackage{expl3}

\ExplSyntaxOn
\tl_new:N \inkscapebanner
\tl_new:N \inkscapeversion
\tl_new:N \inkscapebuild
\tl_new:N \inkscaperelease

\sys_shell_get:nnN { inkscape~-V } { \char_set_catcode_space:n { `~ } } \inkscapebanner
\seq_set_split:NnV \l_tmpa_seq { ~ } \inkscapebanner
\tl_set:Nx \inkscapeversion { \seq_item:Nn \l_tmpa_seq { 2 } }
\tl_set:Nx \inkscapebuild { \seq_item:Nn \l_tmpa_seq { 3 } }
\tl_set:Nx \inkscapebuild { \tl_range:Nnn \inkscapebuild { 1 } { -1 } }
\tl_set:Nx \inkscaperelease { \seq_item:Nn \l_tmpa_seq { 4 } }

\ExplSyntaxOff

\begin{document}

\inkscapebanner\par
\inkscapeversion\par
\inkscapebuild\par
\inkscaperelease\par

\end{document}
egreg
  • 1,121,712
  • 1
    Is the fact that \input|"inkscape -V" works a TeX feature or a side effect of its implementation? A search through the TeXbook yielded nothing and this isn't really how I thought the pipe command worked either (since a pipe usually directs output from its left to the command to its right). Why does this work? – schtandard May 16 '19 at 22:58
  • 2
    @schtandard It's a “TeX Live addition” added a few years ago. – egreg May 16 '19 at 23:18
  • 3
    @schtandard You can find it with texdoc web2c, section 4.5 – egreg May 16 '19 at 23:28
  • Thanks for the pointer! (Actually, MiKTeX doesn't find anything when asking texdoc web2c, but the manual is also available online.) – schtandard May 16 '19 at 23:39