TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have created the following two commands:

\newcommand{\termExplanation}[1]{\item[#1]\label{#1}}
\newcommand{\newTerm}[1]{\hyperref[#1]{\emph{#1}}}

In my text, I use \newTerm{Term 3}, and somewhere else in the document is my term explanation:

\begin{description}
    \termExplanation{Term 1} description...
    \termExplanation{Term 2} description...
    \termExplanation{Term 3} description...
    ...
\end{description}

Working fine so far. But now I wanted to introduce the new Term C#. Of course I used \newTerm{C\#} and \termExplanation{C\#}.
But it fails:

% occurs at \newTerm{C\#}
Missing \endcsname inserted. ..., die Programmiersprache ist \newTerm{C\#}
Extra \endcsname. ..., die Programmiersprache ist \newTerm{C\#}
% occurs at \termExplanation{C\#}
Missing \endcsname inserted. ....1}{8}{Begriffserklärung}{section.A.1}{}}
Extra \endcsname. ....1}{8}{Begriffserklärung}{section.A.1}{}}

When compiling for the first time, the text has two C#, the second one is emphasized and there is no link on either one. The explanation output is just fine. When recompiling there is no pdf output.

What am I doing wrong?

share|improve this question
1  
You can't use \# in a label. – egreg Feb 26 at 12:26
    
that's sad.... thought i could avoid writing C-Sharp – TheFlow0360 Feb 26 at 12:29
up vote 5 down vote accepted

The problem is that the content of \label doesn't handle these special characters well. The easiest alternative would be to simply not use the special characters and define variants of your commands that allow an arbitrary label in such case:

\documentclass{article}
\usepackage{hyperref}

\makeatletter
\def\termExplanation{%
  \@ifnextchar[{\termExplanation@opt}{\termExplanation@noopt}
}
\def\termExplanation@opt[#1]#2{%
  \item[#2]\label{#1}%
}
\def\termExplanation@noopt#1{\termExplanation@opt[#1]{#1}}

\def\newTerm{%
  \@ifnextchar[{\newTerm@opt}{\newTerm@noopt}%
}
\def\newTerm@opt[#1]#2{%
  \hyperref[#1]{\emph{#2}}%
}
\def\newTerm@noopt#1{\newTerm@opt[#1]{#1}}
\makeatother

\begin{document}
\begin{description}
\termExplanation[chash]{C\#} Hello, World!
\termExplanation{lorem} Lorem Ipsum!
\end{description}

\newTerm[chash]{C\#} \newTerm{lorem}
\end{document}

To understand what this bit of code those, I recommend you have a look at:

as they provide very comprehensive answers. Otherwise, the \def command is the TeX primitive behind \newcommand and offers a little more flexibility in the way arguments can be given, though definitely in most cases use \newcommand.

A better option alternative, as suggested by egreg in the comments, is:

\makeatletter
\newcommand\termExplanation{\@dblarg\termExplanation@opt}
\def\termExplanation@opt[#1]#2{\item[#2]\label{#1}}
\newcommand\newTerm{\@dblarg\newTerm@opt}
\def\newTerm@opt[#1]#2{\hyperref[#1]{\emph{#2}}}
\makeatother

As a last note, it really seems to me like you are creating a glossary. If so, then you should also have a look at the glossaries package. I provide a small MWE of the glossaries package below:

Main file:

\documentclass{article}

\usepackage{hyperref}
\usepackage[toc]{glossaries}

\renewcommand*{\glstextformat}[1]{\emph{#1}}
\loadglsentries{glossary}

\makeglossaries

\begin{document}

You can now refer to glossary terms with \gls{permittivity} and
\gls{permeability}.  You can even get the plural with \glspl{permittivity} and
get the capitalized version with \Gls{permittivity} and \Glspl{permeability}.

\clearpage
\printglossaries

\end{document}

and glossary.tex:

\newglossaryentry{permeability}{
    name={permeability},
    plural={permeabilities},
    symbol={\(\mu\)},
    parent={},
    description={A measure of how easy it is to form a magnetic field in a medium.}
}

\newglossaryentry{permittivity}{
    name={permittivity},
    plural={permittivities},
    symbol={\(\varepsilon\)},
    parent={},
    description={A measure of the resistance encountered when forming an electric field in a medium}
}

and the output:

output1 output2

Note that the glossaries package requires an additional run of makeglossaries in order to process the glossary entries.

share|improve this answer
    
is glossaries easy to use? I struggled with integrating bibtex before, so now I hesitate to use big packages when i can achieve my wanted result with some easy macros – TheFlow0360 Feb 26 at 12:33
    
and also, could you give a little explanation to your answer? I' m not that familiar with LaTeX, e.g. I don't know what \makeatletter and \makeatother do ^^ – TheFlow0360 Feb 26 at 12:35
    
your solution doesn't work: Illegal parameter number in definition of \termExplanation@noopt. ...anation@noopt#1{\termExplanation@opt[#1]#2 - I tried to resolve this myself bad had no luck, don't know the syntax that well – TheFlow0360 Feb 26 at 12:43
1  
@TheFlow0360 I updated my answer to include a minimal example of how to use glossaries. Also, I there was a typo in one of the definitions where it should've been [#1]#1 but instead I wrote [#1]#2. – JP-Ellis Feb 26 at 12:48
    
still not working properly for me, but I think I'll use glossaries anyways, so not a problem - thx for the extensive answer – TheFlow0360 Feb 26 at 12:56

You can't generally use \# in a label, because it doesn't expand to a character. However, in this particular situation a workaround is possible:

\documentclass{article}
\usepackage{hyperref}

\newcommand{\termExplanation}[1]{%
  \item[#1]%
  \begingroup
  % locally disable \#
  \def\#{?hashmark?}%
  \phantomsection\label{#1}%
  \endgroup
}
\newcommand{\newTerm}[1]{%
  \begingroup
  % locally disable \#
  \def\#{?hashmark?}%
  \edef\x{\endgroup\noexpand\hyperref[#1]}\x{\emph{#1}}%
}


\begin{document}
\begin{description}
\termExplanation{C\#} Hello, World!
\termExplanation{lorem} Lorem Ipsum!
\end{description}

\newTerm{C\#} \newTerm{lorem}
\end{document}

The trick is to tell LaTeX that \# in a label writes ?hashmark?; the same trick is used to retrieve the label before \hyperref does its job.

Don't forget \phantomsection that creates a suitable anchor.

enter image description here

share|improve this answer
    
\phantomsection is a really good hint, i was missing this - but since I now use glossries anyways, it wasn't that important. Still, thx for your answer – TheFlow0360 Feb 26 at 15:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.