Reputation: 167
I want to:
Read a text file line by line.
Structure given in mwe.
Store in an array and sort them by one of its columns.
Sorted array should appear in a latexfile as normal for perltex.
Far away from this aim, I struggle with the handing over from Perl variables to the latex output.
I had found no analog code in docs and net. Also tag "perltex" is missing here, nevertheless I hope that someone can help.
\documentclass[]{scrartcl}
\usepackage{perltex}
% content of text file "verfile" in same dir
% die & Vers\"ohnung & tr. & vers\"ohnen & in alle R. gehend & fehlt \\
% die & Vergoldung & tr. & vergolden & in alle R. gehend & fehlt \\
% die & Vergeudung & tr. & vergeuden & in alle R. gehend & fehlt \\
\begin{document}
\perlnewcommand{\setline}[1]{$line = $_[0]; return ""} %cp perltex doc p.5
\perlnewcommand{\getline}{$line;}
\perldo{
$verfile = "verfile";
open (VF,"$verfile") || die "$!";
while (<VF>) {
$line = $_; # works: print "$line\n";
}
print "after loop: $line\n";
return "\\setline{$line}";
}
\getline
\end{document}
At the moment I expect only one line to see in the output pdf file for the array work lacks. So the problem remains, Why setline doesn't work?
Upvotes: 2
Views: 157
Reputation: 40778
Here is an example of how you can create a table. This sorts the columns of verfile
according to the content of the first column, and then creates a LaTeX table:
\documentclass{article}
\usepackage{perltex}
\begin{document}
\perlnewcommand{\verfile}[0]{
open (my $VF, '<', 'verfile') or die "$!";
my @lines;
while (my $line = <$VF>) {
chomp $line;
my @fields = split /&/, $line;
push @lines, \@fields;
}
close $VF;
my @sorted = sort {$a->[0] cmp $b->[0]} @lines;
my $result = '
\begin{table}
\centering
\begin{tabular}{cccccc}
';
for my $line (@sorted) {
$result .= (join ' & ', @$line) . "\n";
}
$result .= '\end{tabular}
\end{table}
';
return $result;
} % done
\verfile
\end{document}
Compile this file using perltex --nosafe test.tex
Upvotes: 2