Slightly connected to my previous question (Output of PerlTex with current TeXlive on Windows), I have managed to generate .pdf files with PerlTeX (precisely, with option --latex=lualatex).
Next step to use this wonderful tool fully would be to be able to use it along with other tools I am already using and automate document creation.
As stated in PerlTeX documentation, page 8, I am compiling document with options:
--latex=lualatex --makesty
With that, file noperltex.sty is created. Then, after commenting out \usepackage{perltex} and adding \usepackage{noperltex}, the document can be compiled with only lualatex, not requiring perltex to compile. If I would need to compile document twice, but first with perltex, then with only lualatex, is there a way, how to do that conditionally?
If I may, it would be great to have solution involving arara, making the compilation also automatic.
I have already created a rule for arara to work with perltex (my first attempt, based on biber rule and rule for pythontex, so its very basic), in case of arara solution please consider next code:
perltex.yaml
!config
# perltex rule for arara
# author: Tomas Krulis based on Uwe Ziegenhagen
# requires arara 4.0+
identifier: perltex
name: perltex
commands:
- name: PerlTeX for advanced string manipulation
command: >
@{
return getCommand('perltex', options, getBasename(file));
}
arguments:
- identifier: options
flag: >
@{
if (isList(parameters.options)) {
return parameters.options;
}
else {
throwError('I was expecting a list of options.');
}
}
For testing, please consider next MWE:
% arara: perltex: { options: [ '--latex=lualatex', '--makesty' ] }
% arara: lualatex
\documentclass[11pt]{article}
\usepackage{fontspec}
\usepackage{xcolor}
\usepackage[optional]{perltex}
%\usepackage{noperltex}
\ifperl
\perlnewcommand{\reversewords}[1]{join " ", reverse split " ", $_[0]}
\perlnewcommand{\hilbertmatrix}[1]{
my $result = '
\[
\renewcommand{\arraystretch}{1.3}
';
$result .= '\begin{array}{' . 'c' x $_[0] . "}\n";
foreach $j (0 .. $_[0]-1) {
my @row;
foreach $i (0 .. $_[0]-1) {
push @row, ($i+$j) ? (sprintf '\frac{1}{%d}', $i+$j+1) : '1';
}
$result .= join (' & ', @row) . " \\\\\n";
}
$result .= '\end{array}
\]';
return $result;
}
\else
\newcommand{\reversewords}[1]{\color{red} #1}
\newcommand{\hilbertmatrix}[1]{\color{red} #1}
\fi
\begin{document}
\reversewords{Try doing this without Perl!}
\clearpage
\hilbertmatrix{20}
\end{document}
Please note, that document "works" as it is, but only thanks to conditional \ifperl, which simply sets any perlcommand argument to red text. The desired behavior is to compile with perltex and \usepackage{perltex}, then conditionally switch to \usepackage{noperltex}, when the document is compiled only with lualatex; so the \else part of \ifperl is not trigerred.
Thank very much for any insight in this, maybe peculiar, question.
EDIT: Updated MWE based on suggestion of Mrs. Fischer, to test with condition \IfFileExists. With arara, in step 2, of "only" luaLaTeX compilation, I am still getting "undefined control sequence" error with \perlnewcommand ; which leads me to think that still the \usepackage{noperltex} is not being picked up.
More interestingly, if I compile following MWE with PerlTeX only, I get the same error. I also have already noperltex.sty in project folder, with code, that (I think) doesnt involve definitions of \perlnewcommands in form, that could be processed by lualatex.
Looks like noperltex is created at start of perltex compilation, which prevents else part of \IfFileExists triggering. Is that the bug you encountered? Or is it intended?
EDITED MWE:
% arara: perltex: { options: [ '--latex=lualatex', '--makesty', '--nosafe' ] }
% arara: lualatex: { shell: yes }
\documentclass[11pt]{article}
\usepackage{fontspec}
\usepackage{xcolor}
\IfFileExists{noperltex.sty}{%
\usepackage{noperltex}%
}%
{\usepackage[optional]{perltex}}
\perlnewcommand{\reversewords}[1]{join " ", reverse split " ", $_[0]}
\perlnewcommand{\hilbertmatrix}[1]{
my $result = '
\[
\renewcommand{\arraystretch}{1.3}
';
$result .= '\begin{array}{' . 'c' x $_[0] . "}\n";
foreach $j (0 .. $_[0]-1) {
my @row;
foreach $i (0 .. $_[0]-1) {
push @row, ($i+$j) ? (sprintf '\frac{1}{%d}', $i+$j+1) : '1';
}
$result .= join (' & ', @row) . " \\\\\n";
}
$result .= '\end{array}
\]';
return $result;
}
\begin{document}
\reversewords{Try doing this without Perl!}
\clearpage
\hilbertmatrix{20}
\end{document}
\makeatletter\let\plmac@tag\relaxafter loading the (no)perltex package, but before the command as a work-around. – Ulrike Fischer Sep 11 '19 at 09:39