0

I tried to escape the underscore character but the resultant "_" did not look perfect. Then I came across another answer suggesting to use \texttt. However, I see that \texttt is also boldening the text. Is there a way to circumvent this problem? Here is a minimal example to explain what I mean.

\documentclass{article}
\usepackage{amsmath}
\usepackage{tcolorbox}
\begin{document}
\texttt{This\_is\_a\_}test
%There is a clear difference between the fonts of the contents inside \texttt and outside.
\end{document}

Thank you in advance.

  • 2
    You say the resultant "_" did not look perfect. What output are you trying to achieve? – campa Oct 13 at 10:38
  • 2
    \texttt switches to a different (typewriter) font. – Ulrike Fischer Oct 13 at 10:46
1

That’s how the default serif and monospace fonts look. You can change either of them.

For example, New Computer Modern Roman comes in a slightly-heavier Book weight. This MWE pairs the Book weight of New Computer Modern Roman with the Regular weight of New Computer Modern Mono.

\documentclass{article}
\usepackage{fontspec}
\usepackage{amsmath}
\usepackage{tcolorbox}

\setmainfont{NewComputerModernBook}
\setmonofont{NewComputerModernMono}

\begin{document}
\texttt{This\_is\_a\_}test
%There is a clear difference between the fonts of the contents inside \texttt and outside.
\end{document}

New Computer Modern sample

Maybe those specific fonts are not enough of a correction for your liking, but many font families come in multiple weights that you could try.

| improve this answer | |

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.