Fix an Extra `}` Error When Using a Custom Command in `\ul`
At a glance
- 1. Load soul
Load the soul package, which provides \ul.
- 2. Register the command
Tell soul how many mandatory arguments your custom command takes.
- 3. Underline the text
Use the command inside \ul and compile a minimal test.
The \ul command provided by the soul package processes its contents in order to underline text and allow line breaks. A custom command inside that text can confuse the parser unless soul knows how many arguments the command consumes. The resulting error may mention an extra } even when your braces appear balanced.
Register the custom command
After defining the macro, register it with \soulregister:
\soulregister\someCustomCommand{1}
The number is the count of mandatory arguments. Use 0 for a command with no arguments, 1 for one mandatory argument, and so on. Place the registration in the preamble, after loading soul and defining the command.
Here is a complete example you can compile:
\documentclass{article}
\usepackage{soul}
\newcommand{\someCustomCommand}[1]{\textbf{#1}}
\soulregister\someCustomCommand{1}
\begin{document}
This sentence has \ul{an underlined \someCustomCommand{important word}}.
\end{document}
The macro takes one mandatory argument, so it is registered with {1}. If it took two mandatory arguments, the registration would be \soulregister\someCustomCommand{2}. Register the actual command name, without its argument or a trailing space.
Why registration helps
In ordinary text, TeX expands a macro according to its definition and argument structure. \ul has to inspect and reprocess its contents to create underlining that can break across lines. Without registration, it may not know where a custom command’s arguments end. A closing brace belonging to the macro call can then be interpreted as unmatched, producing a misleading brace error.
Registration gives soul the argument count it needs to read the command correctly. It does not change the macro’s definition or the number of arguments you supply when using it.
If the error remains
First check that the number in \soulregister matches the macro definition. For example, \newcommand{\markword}[2]{...} takes two mandatory arguments and needs {2}. Also confirm that the command is registered after it is defined and before the \ul{...} use.
Then test the smallest possible example: the document above, with your real macro substituted in. If it still fails, the macro may use optional arguments, verbatim-like material, math, or other complex operations that soul cannot safely process just by knowing its mandatory-argument count. Some such commands need a different definition or a separate text-friendly version. Avoid placing fragile or non-text content inside \ul unless you have confirmed that it works with your setup.
Common mistakes
- Registering the wrong argument count: Count mandatory arguments in the macro definition, not braces in the text where you call it.
- Registering too early: Load
soul, define the command, and then register it. - Treating registration as a brace fix: Registration helps
soulparse a macro; it does not repair genuinely unmatched braces in the macro definition or its use. - Assuming optional arguments are covered: The simple count is for mandatory arguments. Commands with optional arguments or unusual syntax may need a different approach.
If the macro is only used to format part of a phrase, another option is to simplify or redesign it so the content passed to \ul is ordinary text plus commands known to work with soul. Test the exact output, especially when the underline spans a line break.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.