f



Messing with listings vs. hyperref

In an application I'd like to create several verbatim like environments.
Say I have two different envrionements. I'd like each to use its own 
counter and each to use its own name.

In addition, they should be auto numbered even if the user does not use 
the caption option.

All of this is doable fairly easy.

But if hyperref is then added, I keep getting those pesky

  pdfTeX warning (ext4): destination with the same identifier

warnings. Presumably because somewhere internally in listings the 
'listings' or 'lstnumber' name have been given directly to some hyperref 
command.

Does anyone have ideas as to how one can fix this small snag?

MWE listed below (use pdflatex to get the pdftex warnings)

\documentclass[a4paper]{article}
\usepackage{listings,caption,etoolbox}

\newcounter{foo}
\renewcommand\thefoo{\thesection.\arabic{foo}}
\newcommand\fooname{Foo}
\newcounter{bar}
\renewcommand\thebar{\thesection.\arabic{bar}}
\newcommand\barname{Bar}

\makeatletter

\newtoggle{capactive}
% this is a direct copy of listings caption key. We just add a toggle
% such we can know when it is being used
\lst@Key{caption}\relax{%
   \lstKV@OptArg[{#1}]{#1}%
     {\def\lst@caption{##2}\def\lst@@caption{##1}}%
     \toggletrue{capactive}%
      \let\lst@title\@empty}

% replica of the original caption key without the toggle. We will use this
% internally to be able to auto number our constuctions when there is
% no direct caption.
\lst@Key{nocaption}\relax{\lstKV@OptArg[{#1}]{#1}%
     {\def\lst@caption{##2}\def\lst@@caption{##1}}%
      \let\lst@title\@empty}

% This next bit is caption configuration. Special thanks for the auto
% caption idea to Ulrike Fischer
\DeclareCaptionTextFormat{gobble}{}

\newcommand\SnRCaptionSetupIfEmpty{
 
\iftoggle{capactive}{}{\captionsetup[lstlisting]{labelsep=none,textformat=gobble}}}


\providecommand\letcountercounter[2]{
    \csletcs{c@#1}{c@#2}
    \csletcs{the#1}{the#2}
}
\makeatother

\lstnewenvironment{Foo}[1][]{
   \letcountercounter{lstlisting}{foo}
   \let\lstlistingname\fooname
   \lstset{nocaption=x}
   \lstset{frame=single,#1}
   \SnRCaptionSetupIfEmpty
}{}

\lstnewenvironment{Bar}[1][]{
   \letcountercounter{lstlisting}{bar}
   \let\lstlistingname\barname
   \lstset{nocaption=x}
   \lstset{frame=single,#1}
   \SnRCaptionSetupIfEmpty
}{}


\usepackage{hyperref}

\begin{document}

\section{Test}
\label{sec:test}

\begin{Foo}
   dsfsdf
\end{Foo}

\begin{Bar}
   sdfsdf
\end{Bar}

\begin{Foo}
   sdsdfsdfsdfsdf
\end{Foo}

\end{document}





-- 

/daleif (remove RTFSIGNATURE from email address)

Memoir and mh bundle maintainer
LaTeX FAQ:      http://www.tex.ac.uk/faq
LaTeX book:     http://www.imf.au.dk/system/latex/bog/  (in Danish)
Remember to post minimal examples, see URL below
http://www.minimalbeispiel.de/mini-en.html
0
daleif1329 (544)
8/25/2011 1:57:17 PM
comp.text.tex 39027 articles. 3 followers. Post Follow

6 Replies
1209 Views

Similar Articles

[PageSpeed] 31

Lars Madsen <daleif@RTFMSIGNATUREimf.au.dk> wrote:

> But if hyperref is then added, I keep getting those pesky
> 
>   pdfTeX warning (ext4): destination with the same identifier
> 
> warnings. Presumably because somewhere internally in listings the 
> 'listings' or 'lstnumber' name have been given directly to some hyperref 
> command.

hyperref needs unique counter names and supports \theH<counter>
for a unique definition.

> \newcounter{foo}
> \renewcommand\thefoo{\thesection.\arabic{foo}}
> \newcommand\fooname{Foo}
> \newcounter{bar}
> \renewcommand\thebar{\thesection.\arabic{bar}}
> \newcommand\barname{Bar}

> \providecommand\letcountercounter[2]{
>     \csletcs{c@#1}{c@#2}
>     \csletcs{the#1}{the#2}
> }

\newcommand*{\foobarnumber}[1]{%
  \begingroup
    \global\let\gtmp@theHlstnumber\theHlstnumber
    \toks@\expandafter{\theHlstnumber}%
    \xdef\gtmp@theHlstnumber{#1-\the\toks@}%
  \endgroup
  \let\theHlstnumber\gtmp@theHlstnumber
  \def\theHlstlisting{%
    lst.#1-\csname the\@ifundefined{theH#1}{}{H}#1\endcsname
  }%
}

> \makeatother
> 
> \lstnewenvironment{Foo}[1][]{
>    \letcountercounter{lstlisting}{foo}

  \foobarnumber{foo}%

>    \let\lstlistingname\fooname
>    \lstset{nocaption=x}
>    \lstset{frame=single,#1}
>    \SnRCaptionSetupIfEmpty
> }{}
> 
> \lstnewenvironment{Bar}[1][]{
>    \letcountercounter{lstlisting}{bar}

  \foobarnumber{bar}%

>    \let\lstlistingname\barname
>    \lstset{nocaption=x}
>    \lstset{frame=single,#1}
>    \SnRCaptionSetupIfEmpty
> }{}

-- 
Heiko Oberdiek
0
8/26/2011 4:09:37 AM
Heiko Oberdiek wrote, On 2011-08-26 06:09:
> Lars Madsen <daleif@RTFMSIGNATUREimf.au.dk> wrote:
> 
>> But if hyperref is then added, I keep getting those pesky
>>
>>   pdfTeX warning (ext4): destination with the same identifier
>>
>> warnings. Presumably because somewhere internally in listings the 
>> 'listings' or 'lstnumber' name have been given directly to some hyperref 
>> command.
> 
> hyperref needs unique counter names and supports \theH<counter>
> for a unique definition.
> 
>> \newcounter{foo}
>> \renewcommand\thefoo{\thesection.\arabic{foo}}
>> \newcommand\fooname{Foo}
>> \newcounter{bar}
>> \renewcommand\thebar{\thesection.\arabic{bar}}
>> \newcommand\barname{Bar}
> 
>> \providecommand\letcountercounter[2]{
>>     \csletcs{c@#1}{c@#2}
>>     \csletcs{the#1}{the#2}
>> }
> 
> \newcommand*{\foobarnumber}[1]{%
>   \begingroup
>     \global\let\gtmp@theHlstnumber\theHlstnumber
>     \toks@\expandafter{\theHlstnumber}%
>     \xdef\gtmp@theHlstnumber{#1-\the\toks@}%
>   \endgroup
>   \let\theHlstnumber\gtmp@theHlstnumber
>   \def\theHlstlisting{%
>     lst.#1-\csname the\@ifundefined{theH#1}{}{H}#1\endcsname
>   }%
> }
> 
>> \makeatother
>>
>> \lstnewenvironment{Foo}[1][]{
>>    \letcountercounter{lstlisting}{foo}
> 
>   \foobarnumber{foo}%
> 
>>    \let\lstlistingname\fooname
>>    \lstset{nocaption=x}
>>    \lstset{frame=single,#1}
>>    \SnRCaptionSetupIfEmpty
>> }{}
>>
>> \lstnewenvironment{Bar}[1][]{
>>    \letcountercounter{lstlisting}{bar}
> 
>   \foobarnumber{bar}%
> 
>>    \let\lstlistingname\barname
>>    \lstset{nocaption=x}
>>    \lstset{frame=single,#1}
>>    \SnRCaptionSetupIfEmpty
>> }{}
> 

that worked beautifully, thanks.

-- 

/daleif (remove RTFSIGNATURE from email address)

Memoir and mh bundle maintainer
LaTeX FAQ:      http://www.tex.ac.uk/faq
LaTeX book:     http://www.imf.au.dk/system/latex/bog/  (in Danish)
Remember to post minimal examples, see URL below
http://www.minimalbeispiel.de/mini-en.html
0
daleif1329 (544)
8/26/2011 8:24:10 AM
In my experience, \refstepcounter does give this type of error in pdf mode. I had a similar problem recently with the following.

\newcounter{notecnt}[section]
\newenvironment{note}{%
  \refstepcounter{notecnt}%
  \smallbreak
  \textbf{Note~\thesection.\thenotecnt}\enskip
}{%
  \nobreak\@nobreaktrue
}

I had to get along quickly, and so I tried the following and it has so far worked, but I am not comfortable with it. \@currentlabel and others revert back to their previous states after the environment. Maybe I can find some help here.

\newenvironment{note}{%
  \stepcounter{notecnt}%
  \protected@edef\@currentlabel{\thesection.\thenotecnt}%
  \def\@currentlabelname{note}%
  \def\@currentHref{note.\@currentlabel}%
  \smallbreak
  \textbf{Note~\thesection.\thenotecnt}\enskip
}{%
  \nobreak\@nobreaktrue
}

I don't know why we have global in 

  \global\let\gtmp@theHlstnumber\theHlstnumber
  
and again another global in

  \xdef\gtmp@theHlstnumber{#1-\the\toks@}
0
amusa22 (300)
8/26/2011 6:19:40 PM
Ahmed Musa wrote, On 2011-08-26 20:19:
> In my experience, \refstepcounter does give this type of error in pdf mode. I had a similar problem recently with the following.
> 
> \newcounter{notecnt}[section]
> \newenvironment{note}{%
>   \refstepcounter{notecnt}%
>   \smallbreak
>   \textbf{Note~\thesection.\thenotecnt}\enskip
> }{%
>   \nobreak\@nobreaktrue
> }
> 
> I had to get along quickly, and so I tried the following and it has so far worked, but I am not comfortable with it. \@currentlabel and others revert back to their previous states after the environment. Maybe I can find some help here.
> 
> \newenvironment{note}{%
>   \stepcounter{notecnt}%
>   \protected@edef\@currentlabel{\thesection.\thenotecnt}%
>   \def\@currentlabelname{note}%
>   \def\@currentHref{note.\@currentlabel}%
>   \smallbreak
>   \textbf{Note~\thesection.\thenotecnt}\enskip
> }{%
>   \nobreak\@nobreaktrue
> }
> 
> I don't know why we have global in 
> 
>   \global\let\gtmp@theHlstnumber\theHlstnumber
>   
> and again another global in
> 
>   \xdef\gtmp@theHlstnumber{#1-\the\toks@}

use \phantomsection just before \refstepcounter to give it something to 
anchor to when hyperref is active, that usually works for me

-- 

/daleif (remove RTFSIGNATURE from email address)

Memoir and mh bundle maintainer
LaTeX FAQ:      http://www.tex.ac.uk/faq
LaTeX book:     http://www.imf.au.dk/system/latex/bog/  (in Danish)
Remember to post minimal examples, see URL below
http://www.minimalbeispiel.de/mini-en.html
0
daleif1329 (544)
8/29/2011 7:49:08 AM
Lars Madsen <daleif@RTFMSIGNATUREimf.au.dk> wrote:

> use \phantomsection just before \refstepcounter to give it something to 
> anchor to when hyperref is active, that usually works for me

Usually \refstepcounter is already creating an anchor.

-- 
Heiko Oberdiek
0
8/29/2011 9:10:35 AM
Heiko Oberdiek wrote, On 2011-08-29 11:10:
> Lars Madsen <daleif@RTFMSIGNATUREimf.au.dk> wrote:
> 
>> use \phantomsection just before \refstepcounter to give it something to 
>> anchor to when hyperref is active, that usually works for me
> 
> Usually \refstepcounter is already creating an anchor.
> 

my bad, it is in relation to internal auto generated labels I always use 
\phantomsection in my own constructions

-- 

/daleif (remove RTFSIGNATURE from email address)

Memoir and mh bundle maintainer
LaTeX FAQ:      http://www.tex.ac.uk/faq
LaTeX book:     http://www.imf.au.dk/system/latex/bog/  (in Danish)
Remember to post minimal examples, see URL below
http://www.minimalbeispiel.de/mini-en.html
0
daleif1329 (544)
8/29/2011 9:27:06 AM
Reply: