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:

Sample code with comments

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

  • 1
    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
  • 1
    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.







\setcounter{chapter}{7} % to emulate your numbering

\subsection{Common reference}\label{cr}

This will be pointed to by the link.

// $\commentref{cr}$, CommonReference:
template <class T, class U>
concept bool CommonReference = see below;


enter image description here


This is possible in lstlisting environment, as following

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

\usepackage{listings, xcolor}
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,
commentstyle = \rmfamily,
\usepackage[colorlinks, linkcolor = blue, anchorcolor = blue, citecolor = green]{hyperref}


\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;


You would get

comment and link

  • 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
  • 1
    @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

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.