1

If I have the following:

\lit*{\char`\\ b}

How do I combine this with a \hl (highlight) to get a highlighted version of \b?

(\lit is from the syntax package, and \hl is from soul)

MPP
  • 13

1 Answers1

1

The solution for me is usually to change how soul registers the command you are inputting

\documentclass{article}
\usepackage{syntax}
% \usepackage{xcolor} % for colouring
\usepackage{soul}

\soulregister\lit7 % this tells soul to register \lit as non expanded command. % so the result is \hl{result of lit} \usepackage{lipsum} \begin{document} Example

\lit{Example}

\hl{Example}

\hl{\lit{Example}}

\end{document}

For more details: How to make \hl (highlighting) to automatically place incompatible commands in \mbox?

Edit

According to quark67's comment, you can create a new command \newcommand{\newlit}[1]{\lit*{#1}} as register that one

\documentclass{article}
\usepackage{syntax}
\usepackage{xcolor} % for colouring
\usepackage{soul}

\newcommand{\newlit}[1]{\lit{#1}} \soulregister\newlit7 % this tells soul to register \newlit as non expanded command. Yes the 7 at the end is what does it! % so the result is \hl{result of \lit} \usepackage{lipsum} \begin{document} Example

\lit{Example}

\hl{Example}

\hl{\lit{Example}}

\hl{\newlit{Example}}

\end{document}

anis
  • 1,510
  • 5
  • 13
  • by extending this reasoning, you can register any command that hl will take as an argument, like cite. – anis Jan 18 '23 at 19:38
  • Very good solution. I suggest that you expand your solution to more closely match the OP's request. In the preamble, create a new command (as I'm unable to \soulregister the starred version on \lit directly): \newcommand{\newlit}[1]{\lit*{#1}}. Then \soulregister it: \soulregister\newlit7. Then in the document, use "\hl{\newlit{\char`\ b}}" (and probably uncomment the line "\usepackage{xcolor}" as the OP probably would use "\hl" with a colored background. – quark67 Jan 18 '23 at 19:54
  • 1
    Thanks for the edit, you can modify the comments "register \lit" -> "register \newlit" and "so the result is \hl{result of lit}" -> "so the result is \hl{result of \lit*}" to reflect the changes. – quark67 Jan 18 '23 at 23:15
  • Thanks a lot everyone! – MPP Jan 19 '23 at 13:05
  • @MingPokNg if you accept the answer, please validate it. – anis Jan 19 '23 at 13:21