# [texhax] Problem with the nameref package

Tue May 20 16:05:09 CEST 2014

On 20.05.2014 02:53, ghaverla wrote:
> On Mon, 19 May 2014 20:27:31 +0200
> Heiko Oberdiek <heiko.oberdiek at googlemail.com> wrote:
>
>> The title for the label is written into the .aux file
>> => moving argument.
>> Therefore it is quite dangerous for a package like nameref'
>> to prefer the long title that might contain fragile stuff.
>
> I have very little experience at writing any TeX macro related.
>
> But, I can appreciate how a long title might be more fragile than a
> short one.

The problem is not the length, but fragility. If the optional argument
of \chapter and friends are used, then the optional argument must be
robust, e.g. by adding \protect for fragile macros, ...
But then the mandatory argument losses the constraint and can happily

> So, I did a little experiment in perl, which didn't work out.  But, I
> thought I might as well mention that I tried something.  Might provide
> ideas for a better solution.  :-)
>
> I made up a long title:
> Some Horribly Long Title Involving Gradients of the Stress Tensor in
> High Modulus Carbon Reinforced DGEBA Based Epoxies Functionalised with
> Single-Walled Carbon Nanontubes and Core Shell Rubber Additives
>
> which is all ASCII, and 203 characters long.  LZW compression is
> supposed to work well on text, but all I got was about 10%
> compression.

#!/usr/bin/env perl
use Compress::Zlib;

chomp(my $title = <<'END_TITLE'); Some Horribly Long Title Involving Gradients of the Stress Tensor in High Modulus Carbon Reinforced DGEBA Based Epoxies Functionalised with Single-Walled Carbon Nanontubes and Core Shell Rubber Additives END_TITLE my$compressed_title = compress($title, 9); my$l = length($title); my$cl = length($compressed_title); printf "Result: %d -> %d (%g %%)\n",$l, $cl, 100 * ($l - $cl)/$l;
__END__

Result: 203 -> 162 (20.197 %)

I converted that raw binary into hexadecimal, and then

Hexadecimal encoding avoids many problems with special characters,
unmatched curly braces, ... I am using it in package bookmark,
the data in the .aux` file are hexadecimal encoded.

> ran the hexadecimal through conversion from base 16 to base 96 (which
> is a printable string).

The problem are special characters: active characters, parameter
character (#), comment character (%), unmatched curly parentheses,
...

> The final string was longer than the string I
> started with, and I doubt it is any less fragile than the string I
> started with.
>
> Maybe someone can come up with a better idea?

The problem is fragility not length, e.g. try

\documentclass{report}
\begin{document}
\tableofcontents
\chapter[foobarfoobarfoobar]{\newcommand*{\foo}{foobar}\foo\foo\foo}
\end{document}

with and without optional argument.
Without you get:

! Use of \@chapter doesn't match its definition.
\@ifnextchar ... \reserved at d =#1\def \reserved at a {
#2}\def \reserved at b
{#3}\f...
l.5   {\newcommand*{\foo}{foobar}\foo\foo\foo}

?

Yours sincerely
Heiko Oberdiek