5

Is it possible to introduce hexadecimal line numbers in steps of two using the listings package?

In the MWE

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{listings}

\lstset{basicstyle={\ttfamily\small}}
\lstset{keywordstyle={\bfseries}}
\lstset{numbers=left, numberstyle=\sffamily\tiny}

\begin{document}

\begin{lstlisting}[language={[x86masm]Assembler}]
mov ax, [10]
jz 0A
add bx, [11]
dec ax
jmp 02
mov [12], bx
hlt
\end{lstlisting}

\end{document}

I would like to achieve that 00, 02, 04, 06, 08, 0A, 0C is printed instead of arabic line numbers.

2 Answers 2

5

Here is a solution using packages xintbinhex for hexadecimal numbers, calc for counting by steps of 2, and renewing the \thelstnumber command for numbering, as indicated in the listings documentation:

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{listings}
\usepackage{xintbinhex}
\usepackage{calc}

\lstset{basicstyle={\ttfamily\small}}
\lstset{keywordstyle={\bfseries}}
\lstset{numbers=left, numberstyle=\sffamily\tiny}
\renewcommand*\thelstnumber{\ifnum\value{lstnumber}<8 0\fi \xintDecToHex{\the\value{lstnumber}*2}}

\begin{document}

\begin{lstlisting}[language={[x86masm]Assembler},firstnumber=0]
mov ax, [10]
jz 0A
add bx, [11]
dec ax
jmp 02
mov [12], bx
hlt
\end{lstlisting}

\end{document}

enter image description here

1
  • I mixed the approaches by JPG and jfbu approaches by using \newcommand\HexLineNum[1]{\ifnum#1 < 8 0\fi\xintDecToHex{2 * {#1}}} and \lstset{numbers=left, numberstyle=\ttfamily\tiny\HexLineNum}.
    – Matthias
    Commented Jan 26, 2019 at 18:15
5

One could do this with no package and little effort using TeX arithmetic so it is bit overfill to use xintbinhex here but it works. But it does not provide macros to pad to a given number of hex-digits, I needed to code it here.

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{listings}

\lstset{basicstyle={\ttfamily\small}}
\lstset{keywordstyle={\bfseries}}
\lstset{numbers=left, numberstyle=\ttfamily\tiny}

\usepackage{xintbinhex}

\makeatletter
\newcommand\myHexNumber[1]{\ttfamily\tiny
                           \romannumeral\expandafter\myHexNumber@pad
                           \romannumeral0\xintdectohex{#1}.}%
% change 2 into how many hex digits are asked for with leading 0s
% (I guess 2 or 3 ...)
\def\myHexNumber@pad#1.{\xintreplicate{2-\xintLength{#1}}{0}#1}
\makeatother
\begin{document}


\begin{lstlisting}[language={[x86masm]Assembler}, numberstyle=\myHexNumber]
mov ax, [10]
jz 0A
add bx, [11]
dec ax
hjmp 02
mov [12], bx
hlt
\end{lstlisting}

\end{document}

This picture does not really prove it does work... but it does, no doubt about it! But it was done when I erroneously used \sffamily. Now corrected to \ttfamily see next image.

enter image description here

enter image description here

2
  • 1
    another method of padding is to add 256 to number to convert and then remove the leading 1. Or to add 4096 and remove leading 1 if one wants three hex-digits. The method here will silently switch from 2 to 3 or more digits when reaching FF so I think it is better.
    – user4686
    Commented Jan 26, 2019 at 17:33
  • Please see my comment to JPG's answer. I think using numberstyle is more versatile when mixing different styles (e.g., for Assembler, Java etc.) in a single document which occurs in my case.
    – Matthias
    Commented Jan 26, 2019 at 18:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .