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

How to (slightly) modify autoref feature of hyperref package

689 views
Skip to first unread message

FarazHussain

unread,
Mar 13, 2009, 12:12:59 PM3/13/09
to
Hello,

In my document, when referring to a certain section, I'm currently
using \ref and writing "§5.3" instead of "Section 5.3". Now I want to
use autoref to get the link on the entire "§5.3" and not just on
"5.3".

Therefore, I wrote the following command:

\def\sectionautorefname{\S{}}

It works good, except for one thing: it inserts a space after the
section symbol and before the section number. So the reference shown
above looks like "§ 5.3". I also tried "\def\sectionautorefname{\S\ }"
but that also doesn't work.

How do I get rid of this unwanted single space character? Thanks in
advance.

Best Regards,

Faraz Hussain.

Philipp Stephani

unread,
Mar 13, 2009, 1:39:12 PM3/13/09
to
FarazHussain schrieb:

> It works good, except for one thing: it inserts a space after the
> section symbol and before the section number. So the reference shown
> above looks like "§ 5.3". I also tried "\def\sectionautorefname{\S\ }"
> but that also doesn't work.
>
> How do I get rid of this unwanted single space character? Thanks in
> advance.

Hello,

unfortunately this is hard-wired, so you have to do a bit of hacking:

\documentclass{article}
\usepackage{hyperref}
\makeatletter
\renewcommand*\sectionautorefname{\S\@gobble}
\makeatother
\begin{document}
\section{Foo}
\label{Foo}
\autoref{Foo}
\end{document}

The \@gobble removes the space that is inserted by hyperref.

--
Replace “READ-MY-SIG” by “tcalveu” to answer by mail.

Ulrich D i e z

unread,
Mar 13, 2009, 3:13:28 PM3/13/09
to
Faraz Hussain wrote:

> \def\sectionautorefname{\S{}}

hyperref automatically inserts a "~" (space without
line-break-permission) between the autorefname and
the number.

You could have removed that "~" by \@gobble.

Another possibility is using as the last token of your
autorefname a macro which does not take arguments
but whose parameter-text is delimited by a "~" .

In case something goes wrong, \@gobble would remove
just something which might lead to unpredictable
results and probably later on strange error-messages.
A macro with delimited parameter-text immediately
delivers a "does not match its definition"-error.

Ulrich


\documentclass{article}
\usepackage{hyperref}
\newcommand\myS[1]{}%<-launch error in case \myS is already defined.
\def\myS~{\S{}}
\renewcommand*\sectionautorefname{\myS}

FarazHussain

unread,
Mar 13, 2009, 3:54:51 PM3/13/09
to
Thanks a lot Phillip. It works perfectly.

FarazHussain

unread,
Mar 13, 2009, 4:02:14 PM3/13/09
to
Hi Ulrich,

On Mar 13, 3:13 pm, Ulrich D i e z <eu_angel...@web.de.invalid>
wrote:

>
> hyperref automatically inserts a "~" (space without
> line-break-permission) between the autorefname and
> the number.
>
> You could have removed that "~" by \@gobble.
>
> Another possibility is using as the last token of your
> autorefname a macro which does not take arguments
> but whose parameter-text is delimited by a "~" .

Are you talking about the ~ inside "\def\myS~{\S{}}"? Please tell me
what its for. I think the definition would be fine even without
it ...?

>
> In case something goes wrong, \@gobble would remove
> just something which might lead to unpredictable
> results and probably later on strange error-messages.
> A macro with delimited parameter-text immediately
> delivers a "does not match its definition"-error.

Okay. And I just tried your suggested method using my own macro and
its working great. Thanks!

Philipp Stephani

unread,
Mar 13, 2009, 4:21:51 PM3/13/09
to
FarazHussain schrieb:

> Hi Ulrich,
>
> On Mar 13, 3:13 pm, Ulrich D i e z <eu_angel...@web.de.invalid>
> wrote:
>
>> hyperref automatically inserts a "~" (space without
>> line-break-permission) between the autorefname and
>> the number.
>>
>> You could have removed that "~" by \@gobble.
>>
>> Another possibility is using as the last token of your
>> autorefname a macro which does not take arguments
>> but whose parameter-text is delimited by a "~" .
>
> Are you talking about the ~ inside "\def\myS~{\S{}}"? Please tell me
> what its for.

This definition implies that every occurrence of \myS has to be followed
by ~.
\myS ~
will work, while
\myS 123
will give an error ("Usage of \myS doesn't match its definition"). Now
hyperref inserts the contents of \sectionautorefname, followed by a ~.
The \myS command checks if it is followed by this ~ and then removes it.
If hyperref was changed in a way so that \sectionautorefname would be
followed by \SomeImportantCommand, this definition of \myS would give an
error. The solution with \@gobble would silently remove
\SomeImportantCommand.

FarazHussain

unread,
Mar 13, 2009, 6:08:54 PM3/13/09
to
On Mar 13, 4:21 pm, Philipp Stephani <READ-MY-...@arcor.de> wrote:
> FarazHussain schrieb:
>
>
>
>
> This definition implies that every occurrence of \myS has to be followed
> by ~.
> \myS ~
> will work, while
> \myS 123
> will give an error ("Usage of \myS doesn't match its definition"). Now
> hyperref inserts the contents of \sectionautorefname, followed by a ~.
> The \myS command checks if it is followed by this ~ and then removes it.
> If hyperref was changed in a way so that \sectionautorefname would be
> followed by \SomeImportantCommand, this definition of \myS would give an
> error. The solution with \@gobble would silently remove
> \SomeImportantCommand.
>

Ah, I get it now... thanks a lot again!

0 new messages