Hexadecimal line numbers in listings












3















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={ttfamilysmall}}
lstset{keywordstyle={bfseries}}
lstset{numbers=left, numberstyle=sffamilytiny}

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.










share|improve this question





























    3















    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={ttfamilysmall}}
    lstset{keywordstyle={bfseries}}
    lstset{numbers=left, numberstyle=sffamilytiny}

    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.










    share|improve this question



























      3












      3








      3


      0






      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={ttfamilysmall}}
      lstset{keywordstyle={bfseries}}
      lstset{numbers=left, numberstyle=sffamilytiny}

      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.










      share|improve this question
















      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={ttfamilysmall}}
      lstset{keywordstyle={bfseries}}
      lstset{numbers=left, numberstyle=sffamilytiny}

      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.







      listings code line-numbering






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 26 at 21:02









      Jonas Stein

      3,20142644




      3,20142644










      asked Jan 26 at 17:04









      MatthiasMatthias

      841515




      841515






















          2 Answers
          2






          active

          oldest

          votes


















          3














          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={ttfamilysmall}}
          lstset{keywordstyle={bfseries}}
          lstset{numbers=left, numberstyle=sffamilytiny}
          renewcommand*thelstnumber{ifnumvalue{lstnumber}<8 0fi xintDecToHex{thevalue{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






          share|improve this answer
























          • I mixed the approaches by JPG and jfbu approaches by using newcommandHexLineNum[1]{ifnum#1 < 8 0fixintDecToHex{2 * {#1}}} and lstset{numbers=left, numberstyle=ttfamilytinyHexLineNum}.

            – Matthias
            Jan 26 at 18:15



















          4














          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={ttfamilysmall}}
          lstset{keywordstyle={bfseries}}
          lstset{numbers=left, numberstyle=ttfamilytiny}

          usepackage{xintbinhex}

          makeatletter
          newcommandmyHexNumber[1]{ttfamilytiny
          romannumeralexpandaftermyHexNumber@pad
          romannumeral0xintdectohex{#1}.}%
          % change 2 into how many hex digits are asked for with leading 0s
          % (I guess 2 or 3 ...)
          defmyHexNumber@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






          share|improve this answer



















          • 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
            Jan 26 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
            Jan 26 at 18:17











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "85"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f471993%2fhexadecimal-line-numbers-in-listings%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          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={ttfamilysmall}}
          lstset{keywordstyle={bfseries}}
          lstset{numbers=left, numberstyle=sffamilytiny}
          renewcommand*thelstnumber{ifnumvalue{lstnumber}<8 0fi xintDecToHex{thevalue{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






          share|improve this answer
























          • I mixed the approaches by JPG and jfbu approaches by using newcommandHexLineNum[1]{ifnum#1 < 8 0fixintDecToHex{2 * {#1}}} and lstset{numbers=left, numberstyle=ttfamilytinyHexLineNum}.

            – Matthias
            Jan 26 at 18:15
















          3














          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={ttfamilysmall}}
          lstset{keywordstyle={bfseries}}
          lstset{numbers=left, numberstyle=sffamilytiny}
          renewcommand*thelstnumber{ifnumvalue{lstnumber}<8 0fi xintDecToHex{thevalue{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






          share|improve this answer
























          • I mixed the approaches by JPG and jfbu approaches by using newcommandHexLineNum[1]{ifnum#1 < 8 0fixintDecToHex{2 * {#1}}} and lstset{numbers=left, numberstyle=ttfamilytinyHexLineNum}.

            – Matthias
            Jan 26 at 18:15














          3












          3








          3







          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={ttfamilysmall}}
          lstset{keywordstyle={bfseries}}
          lstset{numbers=left, numberstyle=sffamilytiny}
          renewcommand*thelstnumber{ifnumvalue{lstnumber}<8 0fi xintDecToHex{thevalue{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






          share|improve this answer













          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={ttfamilysmall}}
          lstset{keywordstyle={bfseries}}
          lstset{numbers=left, numberstyle=sffamilytiny}
          renewcommand*thelstnumber{ifnumvalue{lstnumber}<8 0fi xintDecToHex{thevalue{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







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 26 at 17:38









          JPGJPG

          1,507413




          1,507413













          • I mixed the approaches by JPG and jfbu approaches by using newcommandHexLineNum[1]{ifnum#1 < 8 0fixintDecToHex{2 * {#1}}} and lstset{numbers=left, numberstyle=ttfamilytinyHexLineNum}.

            – Matthias
            Jan 26 at 18:15



















          • I mixed the approaches by JPG and jfbu approaches by using newcommandHexLineNum[1]{ifnum#1 < 8 0fixintDecToHex{2 * {#1}}} and lstset{numbers=left, numberstyle=ttfamilytinyHexLineNum}.

            – Matthias
            Jan 26 at 18:15

















          I mixed the approaches by JPG and jfbu approaches by using newcommandHexLineNum[1]{ifnum#1 < 8 0fixintDecToHex{2 * {#1}}} and lstset{numbers=left, numberstyle=ttfamilytinyHexLineNum}.

          – Matthias
          Jan 26 at 18:15





          I mixed the approaches by JPG and jfbu approaches by using newcommandHexLineNum[1]{ifnum#1 < 8 0fixintDecToHex{2 * {#1}}} and lstset{numbers=left, numberstyle=ttfamilytinyHexLineNum}.

          – Matthias
          Jan 26 at 18:15











          4














          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={ttfamilysmall}}
          lstset{keywordstyle={bfseries}}
          lstset{numbers=left, numberstyle=ttfamilytiny}

          usepackage{xintbinhex}

          makeatletter
          newcommandmyHexNumber[1]{ttfamilytiny
          romannumeralexpandaftermyHexNumber@pad
          romannumeral0xintdectohex{#1}.}%
          % change 2 into how many hex digits are asked for with leading 0s
          % (I guess 2 or 3 ...)
          defmyHexNumber@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






          share|improve this answer



















          • 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
            Jan 26 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
            Jan 26 at 18:17
















          4














          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={ttfamilysmall}}
          lstset{keywordstyle={bfseries}}
          lstset{numbers=left, numberstyle=ttfamilytiny}

          usepackage{xintbinhex}

          makeatletter
          newcommandmyHexNumber[1]{ttfamilytiny
          romannumeralexpandaftermyHexNumber@pad
          romannumeral0xintdectohex{#1}.}%
          % change 2 into how many hex digits are asked for with leading 0s
          % (I guess 2 or 3 ...)
          defmyHexNumber@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






          share|improve this answer



















          • 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
            Jan 26 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
            Jan 26 at 18:17














          4












          4








          4







          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={ttfamilysmall}}
          lstset{keywordstyle={bfseries}}
          lstset{numbers=left, numberstyle=ttfamilytiny}

          usepackage{xintbinhex}

          makeatletter
          newcommandmyHexNumber[1]{ttfamilytiny
          romannumeralexpandaftermyHexNumber@pad
          romannumeral0xintdectohex{#1}.}%
          % change 2 into how many hex digits are asked for with leading 0s
          % (I guess 2 or 3 ...)
          defmyHexNumber@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






          share|improve this answer













          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={ttfamilysmall}}
          lstset{keywordstyle={bfseries}}
          lstset{numbers=left, numberstyle=ttfamilytiny}

          usepackage{xintbinhex}

          makeatletter
          newcommandmyHexNumber[1]{ttfamilytiny
          romannumeralexpandaftermyHexNumber@pad
          romannumeral0xintdectohex{#1}.}%
          % change 2 into how many hex digits are asked for with leading 0s
          % (I guess 2 or 3 ...)
          defmyHexNumber@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







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 26 at 17:21







          user4686















          • 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
            Jan 26 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
            Jan 26 at 18:17














          • 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
            Jan 26 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
            Jan 26 at 18:17








          1




          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
          Jan 26 at 17:33







          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
          Jan 26 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
          Jan 26 at 18:17





          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
          Jan 26 at 18:17


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f471993%2fhexadecimal-line-numbers-in-listings%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          'app-layout' is not a known element: how to share Component with different Modules

          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

          WPF add header to Image with URL pettitions [duplicate]