1

I am trying to parse balanced text (actually, text written in LaTeX) using extract_bracketed from Text::Balanced. However, I did not get a correct match with the following code:

use Text::Balanced qw(extract_bracketed);

my $data = 'xxx \footnote{...} yyy';
(my $ext, my $rem, my $pre) = extract_bracketed($data, '{}', '\footnote');
print "\$ext = $ext\n";
print "\$rem = $rem\n";
print "\$pre = $pre\n";

This prints:

$ext =                                                                                                                  
$rem = xxx \footnote{...} yyy                                                                                           
$pre =

According to the documentation, this output means that a failure occurred, but I do not understand why.

What I actually want to extract is ..., i.e. the contents of the \footnote command.

Why is this happening and how can I fix it?

0
3

Text::Balanced sets $@ on failure so you can get details about the cause:

use strict;
use warnings 'all';
use 5.010;

use Text::Balanced qw(extract_bracketed);

my $text = 'xxx \footnote{...} yyy';
my ($substring, $remainder, $prefix) = extract_bracketed($text, '{}', '\footnote');

warn $@ if $@;

Output:

Did not find prefix: /\footnote/, detected at offset 0 at balanced line 12.

The prefix didn't match because:

  • it has to match from the beginning of the string all the way to the first occurrence of the delimiter

  • \f matches a form feed, not a literal backslash followed by the letter f

The following prefix matches everything up to the first curly brace:

use strict;
use warnings 'all';
use 5.010;

use Text::Balanced qw(extract_bracketed);

my $text = 'xxx \footnote{...} yyy';

my ($substring, $remainder, $prefix) = extract_bracketed($text, '{}', '[^{}]*');
say "<$_>" for $prefix, $substring, $remainder;

Output:

<xxx \footnote>
<{...}>
< yyy>

To actually remove a nested footnote tag from the text, leaving its contents, you need to use extract_tagged:

use strict;
use warnings 'all';
use 5.010;

use Text::Balanced qw(extract_tagged);

my $text = '\footnote{abc \footnote{...} def \emph{!!!} ghi}';

my @pieces = extract_tagged(
    $text, 
    '\\\footnote{', 
    '}', 
    '(?s).*\\\footnote{.*(?=\\\footnote{)'
);

my ($remainder, $prefix, $contents) = @pieces[1, 2, 4];

say $prefix . $contents . $remainder;

Output:

\footnote{abc ... def \emph{!!!} ghi}

Note that this approach works for the simple input you gave, but won't work as a general-purpose LaTeX parser. There are a couple of LaTeX parsers on CPAN, but LaTeX::TOM looks fairly limited and LaTeX::Parser hasn't been updated since 2000.

If you need to do more complex parsing, you may need to write your own parser.

7
  • Thanks very much for your answer. I misunderstood the meaning of "prefix", obviously. The piece of code you provided works for all LaTeX commands, not only \footnote. But what I am interested in is extracting footnotes only... more precisely, what I ultimately want to do is remove any \footnote nested in a \footnote (please do not ask why I am facing such nesting, it is no worth explaining !). Any ideas ? Mar 3 '16 at 19:00
  • @YvesdeSaint-Pern If you change the pattern to '.*\\\footnote' you can extract the contents of the most nested footnote, including the curly braces. Is that what you want? Mar 3 '16 at 23:11
  • I want to convert \footnote{abc \footnote{...} def \emph{!!!} ghi} into: \footnote{abc ... def \emph{!!!} ghi} Mar 4 '16 at 17:09
  • Very interesting but unfortunately the content of the footnote (... in my example above) is removed with your suggestion. I would like to keep it (as plain text, not as a nested footnote). Mar 5 '16 at 18:19
  • I further note that your suggestion does not handle correctly the following string my $text = '\footnote{abc {...} def \emph{!!!} ghi}'; sinces it matches its unique footnote although it is not a nested one... Mar 5 '16 at 19:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.