2

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}

13
  • the question is what is the condition you want to base the compilation on? The existence of noperltex.sty? Then what if the document changes and a newer noperltex.sty would be needed? Commented Sep 11, 2019 at 7:52
  • @UlrikeFischer Thank you for your comment, Mrs. Fischer. The condition, on my part, is exactly the existence of noperltex.sty. It did not occured to me, could it be modified to condition \IfFileExists? (I try). The thought process it was: Incompatibilites and performance precautions. Being beginner (maybe even begginer) in LaTeX, I hit upon mistakes and incompatibilities. I thought, its safer to use PerlTeX only when necessary (to create noperltex.sty or modify it) and then go on only with luaLaTeX. Lastly, I think that PerlTeX is having slightly lower performance. Commented Sep 11, 2019 at 8:16
  • @UlrikeFischer The \ifperl command was meant there as debug tool, to see, if \usepackage{noperltex} is executed or not. Commented Sep 11, 2019 at 8:17
  • testing if noperltex.sty exists should be easy both for arara (@paulocereda can do this) and in the document with \IfFileExists. Btw: I found a bug in perltex, with newer latex the noperltex errors, I already notified the author. Commented Sep 11, 2019 at 8:23
  • @UlrikeFischer Edited MWE based on your suggestion. Is it the bug you reported? If so, how can I monitor resolving the situation? (Is there a project GitHub site or anything like that?) Commented Sep 11, 2019 at 9:18

1 Answer 1

5

Since you are using arara (thank you!), how about the following approach:

  1. Let's create a new rule named writeconfig.yaml which, as the name indicates, creates a configuration file based on the provided text:

    !config
    identifier: writeconfig
    name: WriteConfig
    authors:
    - Paulo Cereda
    commands:
    - name: Writing the configuration file
      command: >
        @{
            writeToFile(toFile(input), text + '\n', append);
            return true;
        }
    arguments:
    - identifier: input
      flag: >
        @{
            return parameters.input;
        }
      required: true
    - identifier: append
      flag: >
        @{
            return isTrue(parameters.append);
        }
      default: false
    - identifier: text
      flag: >
        @{
            return parameters.text;
        }
      required: true
    

    This rule takes at least two parameters, namely:

    • input (required): the name of our configuration file.
    • text (required): the text to be written.
    • append (optional): a flag indicating whether the content should be appended or overwritten (default behaviour is to overwrite the content, as we want to exploit this dea).

    Prior to revision 2, arara 4.0 had a bug when handling boolean values as a return type for commands (as seen in issue #128). Make sure to update arara to at least revision 2 using your TeX distro manager, so the aforementioned rule will have the expected behaviour. You can check the revision by looking at the header when running the tool: arara 4.0 (revision 2)

  2. Change your document and replace these lines:

    \usepackage[optional]{perltex} % for PerlTeX
    \usepackage{noperltex}         % for LuaLaTeX
    

    by this one:

    \input{config.cfg}
    
  3. Now let's create new directives:

    % arara: writeconfig: { input: 'config.cfg', text: '\usepackage[optional]{perltex}' }
    % arara: perltex: { options: [ '--latex=lualatex', '--makesty' ] }
    % arara: writeconfig: { input: 'config.cfg', text: '\usepackage{noperltex}' }
    % arara: lualatex
    
  4. Have fun:

 $ arara mwe.tex 
   __ _ _ __ __ _ _ __ __ _ 
  / _` | '__/ _` | '__/ _` |
 | (_| | | | (_| | | | (_| |
  \__,_|_|  \__,_|_|  \__,_|

 Processing 'mwe.tex' (size: 1003 bytes, last modified: 09/10/2019
 11:03:14), please wait.

 (WriteConfig) Writing the configuration file ............ SUCCESS
 (perltex) PerlTeX for advanced string manipulation ...... SUCCESS
 (WriteConfig) Writing the configuration file ............ SUCCESS
 (LuaLaTeX) LuaLaTeX engine .............................. SUCCESS

 Total: 2.60 seconds

Hope it helps! :)

EDIT: My suggestion is to keep both writeconfig.yaml and perltex.yaml inside your project directory. Then create a file named arararc.yaml (it's one of the four possible names for this file, I usually prefer this one) and add this to it:

!config
paths:
- '.'

It means to search the current project directory for rules first. Then try to run arara from this level.

15
  • I must be having trouble with adding WriteConfig (or the generated config.cfg) to path. I have writeconfig.yaml in current directory, for sure also in 2019/temf-dist (I know its not ideal). My problem might be same as here: tex.stackexchange.com/questions/375841/… But I am having trouble adding current project directory to path in Windows 10. Could you help me with that, with say, the absolute current directory of the project is "C:\User\user\Documents\NewFolder" ? Commented Sep 10, 2019 at 14:57
  • @TomášKruliš I updated the answer and included a possible configuration at the end. Could you check if it works for you? :) Commented Sep 10, 2019 at 15:17
  • Still getting same error message (which I dont understand): The system command execution has failed due to an IO error. Are you sure the provided system command exists in your path? It might be a good idea to check the path and see if the command is available. There are more details available on this exception: DETAILS --------------------------------------------------------- Could not execute [true]. Total: 0.12 seconds C:\texlive\2019\bin\win32\runscript.tlu:907: command failed with exit code 2: java.exe -jar c:\texlive\2019\texmf-dist\scripts\arara\arara.jar mwe Commented Sep 10, 2019 at 15:23
  • @TomášKruliš wait a minute! That's... odd... Let me check something here really quick and I will report back in a minute. Commented Sep 10, 2019 at 15:25
  • 1
    @TomášKruliš hold on, there's a lot of information going on... :) I will try to tidy up something soon.. Commented Sep 10, 2019 at 15:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .