Take the 2-minute tour ×
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.

Possible Duplicate:
Custom command inside soul package's \hl

To make the document easier to understand (and easier to write), I have defined:

\newcommand{\code}[1]{\texttt{#1}}

I have used \code{some code} in hyperref and many many other places and everything has been fine.

However, now I used it with \ul (from soul package), like this:

\ul{\code{Hello}} world

and I get this warning:

! Argument of \code has an extra }.
<inserted text>
                \par
l.52 \ul{\code{Hello}}
                       world

Is theresomething wrong with the way I am defining my command? If I use \ul{\texttt{Hello}} there is no problem. Shouldn't my code be effectively reduced to that?!

Note: I'm not looking for \code{\ul{Hello}} as an answer or similar things that change the text. The main reason is that I generate the text from another file that may decide to put the underline around the code. It would be much more difficult for me to change that order in text, than to fix the definition, or even replace all \codes with \texttts and forget about the macro altogether!

share|improve this question
    
Funny how I didn't find that question! :-/ –  Visa is Racism Mar 5 '12 at 14:55
add comment

marked as duplicate by egreg, diabonas, Torbjørn T., percusse, Werner Mar 5 '12 at 16:51

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

up vote 4 down vote accepted

Have a look at section 5.2 of the soul manual:

To convince soul not to feed font switching (or other) commands to the analyzer, but rather to execute them immediately, they have to be registered, too. The \soulregister macro takes the \soulregister name of a command name and either 0 or 1 for the number of arguments:

\soulregister{<command name>}{<number of arguments>}

That means even though \code is essentially \texttt, you have to make sure that soul treats it appropriately by using \soulregister{\code}{1}.

Minimal working example:

\documentclass{article}
\usepackage{soul}
\newcommand{\code}[1]{\texttt{#1}}
\soulregister{\code}{1}
\begin{document}
\ul{\code{Hello}} world
\end{document}
share|improve this answer
    
Great thanks! Could I ask a side question that is probably related? I see in section 6.4 of the manual how to use soul in section headings. It is very vague, but fine. However, it says how to apply a soul command to the whole section, while I need to write something like \section{Only \ul{this} is underlined}. Is such a thing possible? –  Visa is Racism Mar 5 '12 at 15:00
1  
@Shahbaz Using \ul and friends in sectioning commands should work fine (at least in the standard classes like article etc.). You have to watch out, however, when using this in combination with hyperref: In this case, you'll need to use something like \section{Only \texorpdfstring{\ul{this}}{this} is underlined}. –  diabonas Mar 5 '12 at 15:10
    
You are awesome! –  Visa is Racism Mar 5 '12 at 15:26
add comment

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