Difference between revisions of "Executesystemcommand"

From Wiki
Jump to navigation Jump to search
(→‎Modules which use write18: fix link to gnuplot)
(Template typo)
(3 intermediate revisions by 3 users not shown)
Line 3: Line 3:
 
== How to pass over part of the source to an external program? ==
 
== How to pass over part of the source to an external program? ==
  
There are two ways: <cmd>executesystemcommand</cmd> and
+
There are two ways: {{cmd|executesystemcommand}} and
<cmd>installprogram</cmd>.
+
{{cmd|installprogram}}.
* <cmd>executesystemcommand</cmd> is equal to <cmd>immediate</cmd><cmd>write18</cmd> while
+
* {{cmd|executesystemcommand}} is equal to {{cmd|immediate}}{{cmd|write18}} while
* <cmd>installprogram</cmd> defines a program in the tui file which is then run before the next execution of texexec.
+
* {{cmd|installprogram}} defines a program in the tui file which is then run before the next execution of texexec.
 
I do not know what is the best source of their documenentation.
 
I do not know what is the best source of their documenentation.
  
<cmd>write</cmd> is explained in the texbook, [[write18]] executes its argument on shell.
+
{{cmd|write}} is explained in the TeXbook, [[write18]] executes its argument on shell.
 
luatex will have a better model for executing commands on the shell, and some
 
luatex will have a better model for executing commands on the shell, and some
 
of it is explained in the luatex manual. (But luatex is still pre-alpha, so
 
of it is explained in the luatex manual. (But luatex is still pre-alpha, so
 
the interface can change).
 
the interface can change).
  
<cmd>installprogram</cmd> is not documented, you need to read [[source:core-uti.tex|core-uti.tex]] and
+
{{cmd|installprogram}} is not documented, you need to read {{src|core-uti.tex}} and
[[source:texutl.rb|texutil.rb]] to understand how it works.
+
{{src|texutil.rb}} to understand how it works.
  
 
I found ConTeXt sources to be the best way to understand how these things are
 
I found ConTeXt sources to be the best way to understand how these things are
 
working. See [[modules:t-lilypond|t-lilypond]], which checks if [[write18]] is enabled or not, and then
 
working. See [[modules:t-lilypond|t-lilypond]], which checks if [[write18]] is enabled or not, and then
uses <cmd>executesystemcommand</cmd> or <cmd>installprogram</cmd>:
+
uses {{cmd|executesystemcommand}} or {{cmd|installprogram}}:
  
 
<texcode>
 
<texcode>
Line 38: Line 38:
  
 
The module writes stuff to a temporary file, and then uses lilypond to get a ps/pdf which
 
The module writes stuff to a temporary file, and then uses lilypond to get a ps/pdf which
is included back in the document. Another example is [[source:m-r.tex|m-r.tex]] which writes
+
is included back in the document. Another example is {{src|m-r.tex}} which writes
 
things to a temp file and runs them through R (a statistical program) and
 
things to a temp file and runs them through R (a statistical program) and
 
types the output. It also shows how you can capture the contents of an
 
types the output. It also shows how you can capture the contents of an
 
evironment to a temp file.  The same idea is used in the [[modules:vim|t-vim]] module. Of
 
evironment to a temp file.  The same idea is used in the [[modules:vim|t-vim]] module. Of
course, the ConTeXt sources have plenty of examples. There is also [[source:core-buf.tex|core-buf.tex]]
+
course, the ConTeXt sources have plenty of examples. There is also {{src|core-buf.tex}}
 
where the buffer handling is implemented, and the sources related to metapost
 
where the buffer handling is implemented, and the sources related to metapost
 
handling, which are perhaps the best example of how to go back and forth
 
handling, which are perhaps the best example of how to go back and forth
Line 54: Line 54:
  
 
If you want to read the output verbatim to ConTeXt, you can use
 
If you want to read the output verbatim to ConTeXt, you can use
<code>\typefile{filename}</code>. If the external program outputs tex file,
+
{{cmd|typefile|{filename}}}. If the external program outputs tex file,
then you can use <code>\input{filename}</code>.
+
then you can use {{cmd|input|{filename}}}.
  
 
== Modules which use write18 ==
 
== Modules which use write18 ==
  
* [[source:m-r.tex|m-r.tex]]
+
* {{src|m-r.tex}}
 
* [[modules:t-lilypond|t-lilypond]]
 
* [[modules:t-lilypond|t-lilypond]]
 
* [[modules:vim|t-vim]]
 
* [[modules:vim|t-vim]]
 
* [[modules:gnuplot|t-gnuplot]]
 
* [[modules:gnuplot|t-gnuplot]]
 +
* [[modules:filter|t-filter]]
  
 
[[Category:Inside ConTeXt]]
 
[[Category:Inside ConTeXt]]

Revision as of 12:56, 20 November 2012

See also: write18

How to pass over part of the source to an external program?

There are two ways: \executesystemcommand and \installprogram.

I do not know what is the best source of their documenentation.

\write is explained in the TeXbook, write18 executes its argument on shell. luatex will have a better model for executing commands on the shell, and some of it is explained in the luatex manual. (But luatex is still pre-alpha, so the interface can change).

\installprogram is not documented, you need to read core-uti.tex and texutil.rb to understand how it works.

I found ConTeXt sources to be the best way to understand how these things are working. See t-lilypond, which checks if write18 is enabled or not, and then uses \executesystemcommand or \installprogram:

\def\LP{texmfstart --ifchanged=\lily!filename.tmp --exec bin:lilypond -b eps -dno-gs-load-fonts -dinclude-eps-fonts \lily!filename.tmp}
\def\PDF{texmfstart --ifchanged=\lily!filename.eps pstopdf \lily!filename.eps}
\ifeof18
	\installprogram{\LP}%
	\doif\jobsuffix{pdf}{%
		\installprogram{\PDF}%
	}%
\else
	\executesystemcommand{\LP}%
	\doif\jobsuffix{pdf}{%
		\executesystemcommand{\PDF}%
	}%
\fi

The module writes stuff to a temporary file, and then uses lilypond to get a ps/pdf which is included back in the document. Another example is m-r.tex which writes things to a temp file and runs them through R (a statistical program) and types the output. It also shows how you can capture the contents of an evironment to a temp file. The same idea is used in the t-vim module. Of course, the ConTeXt sources have plenty of examples. There is also core-buf.tex where the buffer handling is implemented, and the sources related to metapost handling, which are perhaps the best example of how to go back and forth between tex and an external program.

Perhaps the R module is easiest to understand. That method will work for most cases where you simply want to capture part of tex source and write it verbatim to a file, and then read the output back into tex.

How to input the result?

If you want to read the output verbatim to ConTeXt, you can use \typefile{filename}. If the external program outputs tex file, then you can use \input{filename}.

Modules which use write18