# How to insert normal-font text inside verbatim?

In the ISO C++ standard papers, blocks of code are displayed in a verbatim-like environment, but the comments inside the code are in normal font. For example:

The comments "7.3.5, CommonReference:" etc. are in normal font, and the links work properly. How is this implemented?

• Welcome to TeX.SX! If I recall correctly you may choose a different font for comments when working with packages like listings. – TeXnician Jul 30 '18 at 13:51
• Please edit your posting to provide an example of a C++ code block with associated comments. That'll make it much easier to provide specific and usable solutions. – Mico Jul 30 '18 at 14:08
• Please also indicate which dialect of C++ you employ. I.e., is it ISO, Visual, GNU, ANSI, or 11? – Mico Jul 30 '18 at 14:21
• OK. BTW, I thought ISO is implied by writing "C++ standard". But clarity is good :-) – L. F. Jul 30 '18 at 14:24
• You could use listings and something like \lstset{language=C++,basicstyle=\ttfamily,commentstyle=\normalfont\itshape}: \documentclass[british]{article} \usepackage[T1]{fontenc} \usepackage[utf8]{inputenc} \usepackage{babel} \usepackage{listings} \lstset{language=C++,basicstyle=\ttfamily,commentstyle=\normalfont\itshape} \begin{document} \begin{lstlisting} // 7.5.3, Rome template <class T, class U> concept bool egg=hatch \end{lstlisting} \end{document} – moewe Jul 30 '18 at 15:16

You can use listings. However, escaping to LaTeX will not preserve the current style (comment, in your case) so I provide a suitable command for the job.

\documentclass{book}
\usepackage{xcolor}
\usepackage{listings}

\usepackage{hyperref}

\lstset{
language=C++,
columns=fullflexible,
basicstyle=\ttfamily,
template <class T, class U>
concept bool CommonReference = see below;
\end{lstlisting}

\end{document}


This is possible in lstlisting environment, as following

% !Mode:: "TeX:UTF-8"
\documentclass[UTF8, english]{article}

\usepackage{listings, xcolor}
\lstset{
tabsize = 4, %% Sets tab space width.
showstringspaces = false, %% Prevents space marking in strings, string is defined as the text that is generally printed directly to the console.
numbers = left, %% Displays line numbers on the left.
keywordstyle = \color{blue}, %% Sets  keyword color.
stringstyle = \color{red}, %% Sets  string color.
rulecolor = \color{black}, %% Sets frame color to avoid being affected by text color.
basicstyle = \small \ttfamily , %% Sets listing font and size.
breaklines = true, %% Enables line breaking.
numberstyle = \tiny,
}

\begin{document}

\begin{lstlisting}[language = C++, frame = trBL, firstnumber = last, escapeinside = {(*@}{@*)}]
#include <iostream>
// here is the comment and clickable (*@\href{https://ctan.org/pkg/listings}{link}@*) in roman font family
int main()
{
std::cout << "Hello, World!";
return 0;
}
\end{lstlisting}

\end{document}


You would get

• Well, what I actually want: 1) code in monospace 2) comment in normal font 3) links in comment work – L. F. Jul 31 '18 at 1:19
• @L.F. I don't think anyone can give you links in the midst of verbatim text. Are you sure you need verbatim style? – Steven B. Segletes Jul 31 '18 at 10:01
• @L.F. I have updated the code to lstlisting environment which works, except for one small point, the link text, since escaped from the lstlisting environment, is not in normal font, but still clickable. I would be grateful if anyone could help me fix this small little problem. – zyy Aug 1 '18 at 0:32