4

I would like to create 2 commands

\newcommand\deffeat[1]...
\newcommand\reffeat[1] ...

The deffeat command would print out as something like \textbf{F001},

and the reffeat command would reference the deffeat with the same feature name plus have the exact same text.

My current experiment is using the following:

\newcounter{thefeatures}
\setcounter{thefeatures}{1}

\newcommand{\deffeat}[1]{
    \phantomsection[a]
    %XXX: \tag{F~\{cifeatures}}
    \label{#1}
    \textbf{F~\arabic{thefeatures}}
    \addtocounter{thefeatures}{1}
}

It's still missing the spell the same text feature (currently I'm using \ref).

edit current state:

using cleveref

\newcounter{thefeatures}
\setcounter{thefeatures}{0}

\crefname{thefeatures}{feature}{features}

\newcommand{\deffeat}[1]{
    \phantomsection
    \refstepcounter{thefeatures}
    \label{#1}
    \textbf{F\arabic{thefeatures}}
}

\crefformat{thefeatures}{#2F#1#3}

1 Answers1

7

By default, \ref prints only the number of the reference. Take a look at the cleveref package to print the name, too. Some minor customization is required for the new reference type, check \crefname in the cleveref manual.

Note that your example had several minor deficiencies, and also is incomplete. A full MWE would have been better. Anyway, the following pattern should help achieving what you are looking for:

\documentclass{scrartcl}
\pagestyle{empty}

\usepackage{hyperref}
\usepackage{cleveref}

\newcounter{thefeatures}
\setcounter{thefeatures}{0}

\crefname{thefeatures}{feature}{features}

\newcommand{\deffeat}[1]{
    \phantomsection
    \refstepcounter{thefeatures}
    \label{#1}
    \textbf{F~\arabic{thefeatures}}
}

\begin{document}
    \deffeat{MyFeature}

    \deffeat{MyFeature2}

    \Cref{MyFeature,MyFeature2}
\end{document}

Result of compilation

As you can see, the package tries hard to be clever.

See this related question: How to get more complete references.

krlmlr
  • 12,530
  • i dont see how i could use the same text for the definition site and the reference site tho (ideally formated as F followed by 3 digits) –  Nov 01 '12 at 14:10
  • 1
    @Ronny: See Section 7.2 of the cleveref manual on full control of reference formatting. Something along the lines of \crefformat{thefeatures}{#2F\prependzeros[3]{#1}#3} should do the trick, but \prependzeros[3]{#1} is a macro still to be implemented, perhaps using the stringstrings package. – krlmlr Nov 01 '12 at 14:14
  • i implemented the part without prependzeroes to see if it works however i get a undefined reference when doing \Cref{test} after a \deffeat{test} –  Nov 01 '12 at 14:43
  • @Ronny: Does \cref{test} work? Have you used \Crefformat? – krlmlr Nov 01 '12 at 14:51
  • cref wont work , i tried with and without Crefformat –  Nov 01 '12 at 14:52
  • full stop, i found a badly placed usepackage for hyperref i fixed the order and everything works now –  Nov 01 '12 at 14:57
  • 1
    Right: http://tex.stackexchange.com/questions/1863/which-packages-should-be-loaded-after-hyperref-instead-of-before – krlmlr Nov 02 '12 at 08:39