Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Capitalize (titlecase) autoref

1,381 views
Skip to first unread message

Olaf Conradi

unread,
May 22, 2006, 4:46:54 PM5/22/06
to
Hello,

Just like /Citep and /citep, I would like to make /Autoref for /autoref.

So I can start a sentance with: /Autoref describes...

I read http://www.tex.ac.uk/cgi-bin/texfaq2html?label=casechange
and started making a first version, but can't get it to work.

It either does not change the output, or if I modify it a bit, it will
change the label, in stead of what the label eventually expands to.

Can anyone help me?

Below is a minimal example of what I have so far.

Thanks,
-Olaf

\documentclass{article}

\usepackage[overload]{textcase}
\usepackage{hyperref}

\makeatletter
\def\first#1#2@{\MakeTextUppercase{#1}}
\def\rest#1#2@{#2}
\newcommand{\titlecase}[1]{\expandafter\first#1@\expandafter\rest#1@}
\newcommand{\Autoref}[1]{\expandafter\titlecase\expandafter{\autoref{#1}}}
\makeatother

\newcommand{\hi}{hi}

\begin{document}

\section{Hello}
\label{sec:hello}

Test for \titlecase{titlecase} and \titlecase{\hi}. Wow this works.

\section{Bye}

As seen in \autoref{sec:hello}, we can discuss this issue further.
\Autoref{sec:hello} and so on, this sentence should always start with a capital!

\end{document}

Ulrich Diez

unread,
May 23, 2006, 11:38:32 AM5/23/06
to
Olaf Conradi wrote:

> Hello,
>
> Just like /Citep and /citep, I would like to make /Autoref for /autoref.
>
> So I can start a sentance with: /Autoref describes...

...
> Can anyone help me?

Let's have a look at the definition of \autoref:

\DeclareRobustCommand\autoref[1]{%
\@safe@activestrue
\expandafter\auto@setref\csname r@#1\endcsname\@firstoffive{#1}%
\@safe@activesfalse
}%

Now let's have a look at the definition of \auto@setref:

\newcommand*\auto@setref[3]{%
\ifx#1\relax
\protect\G@refundefinedtrue
\nfss@text{\reset@font\bfseries??}%
\@latex@warning{Reference `#3' on page \thepage\space undefined}%
\else
\edef\@thisref{\expandafter\@fourthoffive#1\@empty\@empty}%
\expandafter\test@reftype\@thisref\\%
\hyper@@link{\expandafter\@fifthoffive#1}%
{\expandafter\@fourthoffive#1\@empty\@empty}%
{\@currentHtag\expandafter#2#1\@empty\@empty\null}%
\fi
}%

We find out:

- Therein you find a call to \hyper@@link:
The third argument thereof forms the text.

- Within that third argument you find a call to
\@currentHtag. \@currentHtag "spits out" a
\csname...\endcsname-construct.
This means that before proceeding, \@currentHtag should be
expanded twice. The first expansion delivers the \csname-construct,
the second expansion delivers the token that is to be formed
by the csname-construct. Your titlecase-macro will expand
that token before uppercasing...

Perhaps it is sufficient to change

{\@currentHtag\expandafter#2#1\@empty\@empty\null}%

to

{\expandafter\expandafter
\expandafter \titlecase
\expandafter\expandafter
\expandafter{%
\@currentHtag\expandafter#2#1\@empty\@empty\null}%
}%

Let's test:

\documentclass{article}

\usepackage[overload]{textcase}
\usepackage{hyperref}

\makeatletter

\def\first#1#2@{\MakeTextUppercase{#1}}
\def\rest#1#2@{#2}
\newcommand{\titlecase}[1]{\expandafter\first#1@\expandafter\rest#1@}

\@ifdefinable\Autoref{%
\DeclareRobustCommand\Autoref[1]{%
\@safe@activestrue
\expandafter\Auto@setref\csname r@#1\endcsname\@firstoffive{#1}%
\@safe@activesfalse
}%
\newcommand*\Auto@setref[3]{%
\ifx#1\relax
\protect\G@refundefinedtrue
\nfss@text{\reset@font\bfseries??}%
\@latex@warning{Reference `#3' on page \thepage\space undefined}%
\else
\edef\@thisref{\expandafter\@fourthoffive#1\@empty\@empty}%
\expandafter\test@reftype\@thisref\\%
\hyper@@link{\expandafter\@fifthoffive#1}%
{\expandafter\@fourthoffive#1\@empty\@empty}%
{\expandafter\expandafter
\expandafter \titlecase
\expandafter\expandafter
\expandafter{%
\@currentHtag\expandafter#2#1\@empty\@empty\null
}%
}%
\fi
}%
}%
\makeatother

\newcommand{\hi}{hi}

\begin{document}

\section{Hello}
\label{sec:hello}

Test for \titlecase{titlecase} and \titlecase{\hi}. Wow this
works.

\section{Bye}

As seen in \autoref{sec:hello}, we can discuss this issue further.
\Autoref{sec:hello} and so on, this sentence should always start
with a capital!

\end{document}


Seems to work.

But it will only work as long as these \@currentHtag-thingies
deliver \csname...\endcsname-constructs which in turn yield
tokens that expand to plain text.
As \@currentHtag-thingies either expand to
\csname <counter>autorefname\endcsname
or
\csname <counter>name\endcsname
, the likelihood that things work out as expected is high.
Just don't do things like:
- \def\sectionautorefname{\relax section}
- \def\sectionautorefname{{my nice} section}
- \let\sectionautorefname\undefined
and either of the variants:
\def\sectionname{\relax section}
\def\sectionname{{my nice} section}
[ \let\sectionname\undefined ]

Ulrich

Ulrich Diez

unread,
May 24, 2006, 4:53:27 AM5/24/06
to
Olaf Conradi wrote:

> \documentclass{article}
>
> \usepackage[overload]{textcase}
> \usepackage{hyperref}
>
> \makeatletter
> \def\first#1#2@{\MakeTextUppercase{#1}}
> \def\rest#1#2@{#2}
> \newcommand{\titlecase}[1]{\expandafter\first#1@\expandafter\rest#1@}

Following variant will do the same without probably stripping
braces that might surround the second argument of \rest:

\newcommand{\titlecase}[1]{\expandafter\MakeTextUppercase#1}%

Let's test:

\documentclass{article}

\usepackage[overload]{textcase}
\usepackage{hyperref}

\makeatletter

\newcommand{\titlecase}[1]{\expandafter\MakeTextUppercase#1}%

\makeatother

\newcommand{\hi}{hi}

\begin{document}

\section{Hello}
\label{sec:hello}

Test for \titlecase{titlecase} and \titlecase{\hi}. Wow this
works.

\section{Bye}

As seen in \autoref{sec:hello}, we can discuss this issue further.
\Autoref{sec:hello} and so on, this sentence should always start
with a capital!

\end{document}

Seems to work.
But same restrictions as described in my previous posting.

Ulrich

Olaf Conradi

unread,
May 24, 2006, 2:19:12 PM5/24/06
to
On Wed, 24 May 2006 10:53:27 +0200, Ulrich wrote:
> Following variant will do the same without probably stripping
> braces that might surround the second argument of \rest:
> Seems to work.
>
> But same restrictions as described in my previous posting.

Thank you very much for the explanation and example.
Works very nice.

I'll keep those limitation in redefinitions of *name and *autorefname in
mind. I don't think they will become a problem.

Cheers
-Olaf

0 new messages