1

I'm trying to highlight arbitrary text fragments, in a beamer presentation, in order to explain SQL injection attacks. Using underlining, or a different text color works fine, because only the specified text is affected, but when try to use bold instead, both the specified text and any adjoining punctuation character is affected. I understand it is for typographic reasons, but here I'm trying to put emphasis on characters, not on words, and the result is not what I expect.

Here is an exemple:

\documentclass{beamer}

\usepackage{times}

\newcommand{\highlight}[1]{\textbf{#1}}

\begin{document}

  \begin{frame}
    \frametitle{Presentation}
    \texttt{WHERE user\_id = '\highlight{' OR 1=1 \#}';}
  \end{frame}

\end{document}

The outer quote caracters should not be hightlighted, nor the final semi-colon, only the internal string part.

I tried the following command definitions, without success:

\newcommand{\highlight}[1]{\textbf{#1}}
\newcommand{\highlight}[1]{\bfseries #1}
\newcommand{\highlight}[1]{\bfseries #1 \mdseries}

From this discussion, I understand \bfseries is the lower-level font change primitive, whereas \textbf is the higher-level primitive, and I should probably rely on the second one instead. I also tried to get inspiration from the \textbf macro definition given in comment #52, using the part corresponding to the math environment, as it is seems more strict, but without success, probably because it seems to use plain tex syntax, not latex.

Of course, as other hightlighting strategies works fine, I should probably stop trying to use bold fonts, but that just sound more natural for me than fancy colours. Am I a desesperate case ?

4
  • 1
    \bfseries is not more lower-level, it is a switch while \textbf takes an argument. \textbf{#1} should work fine, show a complete example that can be compiled and tested where it fails. Oct 8 '20 at 18:43
  • Welcome to the site. Can you provide a small but complete piece of compiling code that exhibits the behavior under discussion? Oct 8 '20 at 18:43
  • \newcommand{\highlight}[1]{\bfseries #1} will certainly make all following text bold, the other two will not. Oct 8 '20 at 19:58
  • The standard Computer Modern font has no boldface monospaced font.
    – egreg
    Oct 8 '20 at 21:46
0

Here's an example:

\documentclass{article}

\newcommand{\highlight}[1]{\textbf{#1}}

\begin{document}

\texttt{WHERE user\_id = '\highlight{' OR 1=1 \#}';}

\end{document}

enter image description here

Wait! There's no boldface at all! Yes, if you look in the log file you'll see

LaTeX Font Info:    Font shape `OT1/cmtt/bx/n' in size <10> not available
(Font)              Font shape `OT1/cmtt/m/n' tried instead on input line 9.

because there is no monospaced bold in the Computer Modern fonts. You may use the similar Latin Modern fonts that have the monospaced bold font and also a lighter medium size, for better distinguishing the two weights:

\documentclass{article}
\usepackage[lighttt]{lmodern} % to emphasize boldface

\newcommand{\highlight}[1]{\textbf{#1}}

\begin{document}

\texttt{WHERE user\_id = '\highlight{' OR 1=1 \#}';}

\end{document}

enter image description here

Without the lighttt option you'd get

enter image description here

Or you can use an altogether different monospaced font such as Inconsolata:

\documentclass{article}
\usepackage{inconsolata}

\newcommand{\highlight}[1]{\textbf{#1}}

\begin{document}

\texttt{WHERE user\_id = '\highlight{' OR 1=1 \#}';}

\end{document}

enter image description here

There are many other choices.

1
  • I was using Times fonts, actually, probably as an old habit of always using the same presentation templates from ages. Just switching to lmodern, with the lighttt option, as you suggested, fixed the issue. Oct 11 '20 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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