Using bold/italic text inside listings?












25














How can I use bold text inside a code listing? I wanted to make some parts of the code bold.










share|improve this question
























  • Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers.
    – jubobs
    Mar 1 '14 at 18:28






  • 1




    I found this answer from another question more satisfacting than current answers because it does not define new keywords.
    – Gabriel Devillers
    Jan 5 '18 at 13:40


















25














How can I use bold text inside a code listing? I wanted to make some parts of the code bold.










share|improve this question
























  • Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers.
    – jubobs
    Mar 1 '14 at 18:28






  • 1




    I found this answer from another question more satisfacting than current answers because it does not define new keywords.
    – Gabriel Devillers
    Jan 5 '18 at 13:40
















25












25








25


10





How can I use bold text inside a code listing? I wanted to make some parts of the code bold.










share|improve this question















How can I use bold text inside a code listing? I wanted to make some parts of the code bold.







listings bold italic






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 '12 at 13:28









doncherry

34.7k23135208




34.7k23135208










asked Sep 6 '11 at 12:34









Maiz

126123




126123












  • Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers.
    – jubobs
    Mar 1 '14 at 18:28






  • 1




    I found this answer from another question more satisfacting than current answers because it does not define new keywords.
    – Gabriel Devillers
    Jan 5 '18 at 13:40




















  • Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers.
    – jubobs
    Mar 1 '14 at 18:28






  • 1




    I found this answer from another question more satisfacting than current answers because it does not define new keywords.
    – Gabriel Devillers
    Jan 5 '18 at 13:40


















Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers.
– jubobs
Mar 1 '14 at 18:28




Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers.
– jubobs
Mar 1 '14 at 18:28




1




1




I found this answer from another question more satisfacting than current answers because it does not define new keywords.
– Gabriel Devillers
Jan 5 '18 at 13:40






I found this answer from another question more satisfacting than current answers because it does not define new keywords.
– Gabriel Devillers
Jan 5 '18 at 13:40












5 Answers
5






active

oldest

votes


















20














As per Mico's answer, there is no boldfaced monospaced font in the Computer Modern font family, so you need to use a font that has bold monospaced font. Below is an example using listings that make the keywords bold using the pxffonts. Here is a comparison of the results without and with the usepackage{pxfonts}:



enter image description hereenter image description here



documentclass[border=2pt]{standalone}
usepackage{listings}
usepackage{pxfonts}

lstset{language=C,
basicstyle=ttfamily,
keywordstyle=bfseries,
showstringspaces=false,
morekeywords={include, printf}
}

begin{document}
begin{lstlisting}
/* Prints Hello World */

#include <stdio.h>

int main(void) {
printf("Hello World!");
return 0;
}
end{lstlisting}
end{document}




Alternate Solution:



You could also use the the courier font form ttfamily with bfseries or how to enable bold in fixed width font:



enter image description here



documentclass[border=2pt]{standalone}
usepackage{listings}

lstset{language=C,
basicstyle=ttfamily,
keywordstyle=bfseries,
showstringspaces=false,
morekeywords={include, printf}
}

begin{document}
begin{lstlisting}
/* Prints Hello World */

#include <stdio.h>

int main(void) {
printf("Hello World!");
return 0;
}
end{lstlisting}

hrule
renewcommand{ttdefault}{pcr}
begin{lstlisting}
/* Prints Hello World */

#include <stdio.h>

int main(void) {
printf("Hello World!");
return 0;
}
end{lstlisting}
end{document}





share|improve this answer































    8














    As you must have already discovered, there is no boldfaced monospaced font in the Computer Modern font family. (Computer Modern is what's loaded by default.) To get the option of highlighting part of your code in a boldfaced font, you must switch to a different font family.




    • One way to do this is to add the command usepackage[scaled=1.04]{couriers} to the preamble of your document; doing so will tell (La)TeX to use the Courier monospaced font instead of the Computer-Modern (CM) variety. The ordinary and boldfaced versions of Courier look very different from CM mono, which is presumably what you want. The downside of using Courier is that it's set very loosely, i.e., you can't squeeze as many words into a line as you could with CM mono.


    • A fairly unobtrusive option -- in the sense that the main text and math fonts are virtually indistinguishable from Computer Modern, while having access to a monospaced font that's more compact than Courier -- would be to use the Latin Modern font family, loaded with the command usepackage{lmodern}. While this font family does have a boldface-monospaced font, it is not all that bold(faced) and thus may not meet your needs.



    • Assuming you're willing to depart from the Computer/Latin Modern font family entirely, some good options for font families could be selected with either one of the following two commands: usepackage{pxfonts} (a Palatino font) or usepackage{txfonts} (a Times Roman font). Note that the (variable-spaced) text fonts will look very different, but the creator of these two font families gave them each the same style of monospaced text.




      • The mathptmx package, which gives you still another option for Times New Roman, does not feature a boldfaced monospaced font and hence won't do the job for you.


      • Update Jan 2019: In the meantime, i.e., since I wrote the original version of this answer back in 2011, the txfonts and pxfonts font packages have been superseded (if not entirely replaced) by newtxtext/newtxmath on the one hand and newpxtext/newpxmath on the other. By all means, if you've used the txfonts and/or pxfonts packages in the path, do consider switching to either newtxtext/newtxmath or newpxtext/newpxmath. What hasn't changed is that newtxtext and newpxtext share the same monospaced font -- both medium ("normal") and bold weight.





    The following MWE serves to illustrate the effects of choosing among these options:



    documentclass{article}
    %% Uncomment one (and only one) of the following four usepackage commands:
    %% usepackage[scaled=1.04]{couriers}
    usepackage{lmodern}
    %%usepackage{pxfonts} % Palatino font
    %%usepackage{txfonts} % Times font

    begin{document}
    %Monospaced font:
    texttt{The quick brown fox jumps over the lazy dog.}

    %Monospaced font with some boldfaced words:
    texttt{The quick brown textbf{fox} jumps over the textbf{lazy} dog.}
    end{document}


    Courier mono generates this text:



    enter image description here



    Latin Modern mono looks like this (note the rather minor difference
    between non-bold and bold):



    enter image description here



    Both the pxfonts and txfonts samples look like this:



    enter image description here






    share|improve this answer























    • Why the downvote? I wrote this answer nearly 7 years ago. If some fonts have changed since, kindly inform me what's no longer up-to-date about this answer, and I'll gladly update it. Thanks.
      – Mico
      Apr 12 '18 at 5:24










    • This does not cover italics with monospace, only bold. (I am not the one who down-voted this answer)
      – Mikaël Mayer
      Jul 30 '18 at 16:44










    • @MikaëlMayer - Thanks for this. I still would prefer for whoever saw fit to downvote this answer to provide some usable feedback regarding what irked him/her so much as to create the urge to downvote.
      – Mico
      Jul 30 '18 at 18:25



















    6














    you need a typewriter font which supports bold characters, eg:



    usepackage[scale=0.9]{beramono}


    and inside the listing you can use the escape character. You'l lfind some examples in the documentation, run texdoc listings






    share|improve this answer

















    • 1




      Or you could usepackage{bold-extra} if you want a bold version of cmtt.
      – kahen
      Nov 27 '11 at 21:28










    • no, I don't want the cmtt font ...
      – Herbert
      Nov 27 '11 at 21:38



















    2














    The listings package might suffice.



    Start with:



    usepackage{listings}

    ...

    lstset{morekeywords={example,foo,bar}}

    ...

    begin[language=C]{lstlisting}
    YOUR_CODE_HERE
    end{lstlisting}


    The package allows you to define your own language definition (lstdefinelanguage), and you get some fine-grained control about how the language is presented, so you get some re-usability of your setup (i.e., preferable over manually setting your keywords bold).






    share|improve this answer





















    • Does the listings package provide the option of bold-facing (if desired) complete sentences at a time? Also, can you control when words such as "example", "foo", and "bar" should be typeset in boldface-mono and when they should be printed in the normal mono font?
      – Mico
      Sep 6 '11 at 14:43










    • Beyond my knowledge of the package. Since I do not know what exactly your requirements are (and would have to look up the package documentation myself), I'd recommend you sift through the docs at the link above.
      – DevSolar
      Sep 6 '11 at 16:21










    • I am actually not the person who originally posted the question, and hence I don't either know what his/her specific needs (in terms of bold-facing some words) might be. It just struck me that the morekeywords listing facility might not be flexible enough to meet his/her needs.
      – Mico
      Sep 6 '11 at 18:14










    • Chapter 4.14 Escaping to LaTeX of the listings package documentation provides several ways to embed LaTeX commands in your listing.
      – DevSolar
      Sep 7 '11 at 14:34



















    2














    I can't remember where I came across the following definition to use a boldface typewriter font with Computer Modern:



    % Declare bold typewriter font with Computer Modern
    DeclareFontShape{OT1}{cmtt}{bx}{n}{<5><6><7><8><9><10><10.95><12><14.4><17.28><20.74><24.88>cmttb10}{}


    Then:



    usepackage{listings}
    lstloadlanguages{Matlab}
    lstset{
    basicstyle=scriptsizeupshapettfamily,
    keywordstyle=color{blue}bfseries,
    % etc.
    }


    Result:



    enter image description here



    I'm not an expert in fonts in LaTeX so I'm not entirely sure what the DeclareFontShape command does behind the scenes but I like the result.






    share|improve this answer





















      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%2f27663%2fusing-bold-italic-text-inside-listings%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      5 Answers
      5






      active

      oldest

      votes








      5 Answers
      5






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      20














      As per Mico's answer, there is no boldfaced monospaced font in the Computer Modern font family, so you need to use a font that has bold monospaced font. Below is an example using listings that make the keywords bold using the pxffonts. Here is a comparison of the results without and with the usepackage{pxfonts}:



      enter image description hereenter image description here



      documentclass[border=2pt]{standalone}
      usepackage{listings}
      usepackage{pxfonts}

      lstset{language=C,
      basicstyle=ttfamily,
      keywordstyle=bfseries,
      showstringspaces=false,
      morekeywords={include, printf}
      }

      begin{document}
      begin{lstlisting}
      /* Prints Hello World */

      #include <stdio.h>

      int main(void) {
      printf("Hello World!");
      return 0;
      }
      end{lstlisting}
      end{document}




      Alternate Solution:



      You could also use the the courier font form ttfamily with bfseries or how to enable bold in fixed width font:



      enter image description here



      documentclass[border=2pt]{standalone}
      usepackage{listings}

      lstset{language=C,
      basicstyle=ttfamily,
      keywordstyle=bfseries,
      showstringspaces=false,
      morekeywords={include, printf}
      }

      begin{document}
      begin{lstlisting}
      /* Prints Hello World */

      #include <stdio.h>

      int main(void) {
      printf("Hello World!");
      return 0;
      }
      end{lstlisting}

      hrule
      renewcommand{ttdefault}{pcr}
      begin{lstlisting}
      /* Prints Hello World */

      #include <stdio.h>

      int main(void) {
      printf("Hello World!");
      return 0;
      }
      end{lstlisting}
      end{document}





      share|improve this answer




























        20














        As per Mico's answer, there is no boldfaced monospaced font in the Computer Modern font family, so you need to use a font that has bold monospaced font. Below is an example using listings that make the keywords bold using the pxffonts. Here is a comparison of the results without and with the usepackage{pxfonts}:



        enter image description hereenter image description here



        documentclass[border=2pt]{standalone}
        usepackage{listings}
        usepackage{pxfonts}

        lstset{language=C,
        basicstyle=ttfamily,
        keywordstyle=bfseries,
        showstringspaces=false,
        morekeywords={include, printf}
        }

        begin{document}
        begin{lstlisting}
        /* Prints Hello World */

        #include <stdio.h>

        int main(void) {
        printf("Hello World!");
        return 0;
        }
        end{lstlisting}
        end{document}




        Alternate Solution:



        You could also use the the courier font form ttfamily with bfseries or how to enable bold in fixed width font:



        enter image description here



        documentclass[border=2pt]{standalone}
        usepackage{listings}

        lstset{language=C,
        basicstyle=ttfamily,
        keywordstyle=bfseries,
        showstringspaces=false,
        morekeywords={include, printf}
        }

        begin{document}
        begin{lstlisting}
        /* Prints Hello World */

        #include <stdio.h>

        int main(void) {
        printf("Hello World!");
        return 0;
        }
        end{lstlisting}

        hrule
        renewcommand{ttdefault}{pcr}
        begin{lstlisting}
        /* Prints Hello World */

        #include <stdio.h>

        int main(void) {
        printf("Hello World!");
        return 0;
        }
        end{lstlisting}
        end{document}





        share|improve this answer


























          20












          20








          20






          As per Mico's answer, there is no boldfaced monospaced font in the Computer Modern font family, so you need to use a font that has bold monospaced font. Below is an example using listings that make the keywords bold using the pxffonts. Here is a comparison of the results without and with the usepackage{pxfonts}:



          enter image description hereenter image description here



          documentclass[border=2pt]{standalone}
          usepackage{listings}
          usepackage{pxfonts}

          lstset{language=C,
          basicstyle=ttfamily,
          keywordstyle=bfseries,
          showstringspaces=false,
          morekeywords={include, printf}
          }

          begin{document}
          begin{lstlisting}
          /* Prints Hello World */

          #include <stdio.h>

          int main(void) {
          printf("Hello World!");
          return 0;
          }
          end{lstlisting}
          end{document}




          Alternate Solution:



          You could also use the the courier font form ttfamily with bfseries or how to enable bold in fixed width font:



          enter image description here



          documentclass[border=2pt]{standalone}
          usepackage{listings}

          lstset{language=C,
          basicstyle=ttfamily,
          keywordstyle=bfseries,
          showstringspaces=false,
          morekeywords={include, printf}
          }

          begin{document}
          begin{lstlisting}
          /* Prints Hello World */

          #include <stdio.h>

          int main(void) {
          printf("Hello World!");
          return 0;
          }
          end{lstlisting}

          hrule
          renewcommand{ttdefault}{pcr}
          begin{lstlisting}
          /* Prints Hello World */

          #include <stdio.h>

          int main(void) {
          printf("Hello World!");
          return 0;
          }
          end{lstlisting}
          end{document}





          share|improve this answer














          As per Mico's answer, there is no boldfaced monospaced font in the Computer Modern font family, so you need to use a font that has bold monospaced font. Below is an example using listings that make the keywords bold using the pxffonts. Here is a comparison of the results without and with the usepackage{pxfonts}:



          enter image description hereenter image description here



          documentclass[border=2pt]{standalone}
          usepackage{listings}
          usepackage{pxfonts}

          lstset{language=C,
          basicstyle=ttfamily,
          keywordstyle=bfseries,
          showstringspaces=false,
          morekeywords={include, printf}
          }

          begin{document}
          begin{lstlisting}
          /* Prints Hello World */

          #include <stdio.h>

          int main(void) {
          printf("Hello World!");
          return 0;
          }
          end{lstlisting}
          end{document}




          Alternate Solution:



          You could also use the the courier font form ttfamily with bfseries or how to enable bold in fixed width font:



          enter image description here



          documentclass[border=2pt]{standalone}
          usepackage{listings}

          lstset{language=C,
          basicstyle=ttfamily,
          keywordstyle=bfseries,
          showstringspaces=false,
          morekeywords={include, printf}
          }

          begin{document}
          begin{lstlisting}
          /* Prints Hello World */

          #include <stdio.h>

          int main(void) {
          printf("Hello World!");
          return 0;
          }
          end{lstlisting}

          hrule
          renewcommand{ttdefault}{pcr}
          begin{lstlisting}
          /* Prints Hello World */

          #include <stdio.h>

          int main(void) {
          printf("Hello World!");
          return 0;
          }
          end{lstlisting}
          end{document}






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Apr 13 '17 at 12:34









          Community

          1




          1










          answered Nov 27 '11 at 20:55









          Peter Grill

          164k25437748




          164k25437748























              8














              As you must have already discovered, there is no boldfaced monospaced font in the Computer Modern font family. (Computer Modern is what's loaded by default.) To get the option of highlighting part of your code in a boldfaced font, you must switch to a different font family.




              • One way to do this is to add the command usepackage[scaled=1.04]{couriers} to the preamble of your document; doing so will tell (La)TeX to use the Courier monospaced font instead of the Computer-Modern (CM) variety. The ordinary and boldfaced versions of Courier look very different from CM mono, which is presumably what you want. The downside of using Courier is that it's set very loosely, i.e., you can't squeeze as many words into a line as you could with CM mono.


              • A fairly unobtrusive option -- in the sense that the main text and math fonts are virtually indistinguishable from Computer Modern, while having access to a monospaced font that's more compact than Courier -- would be to use the Latin Modern font family, loaded with the command usepackage{lmodern}. While this font family does have a boldface-monospaced font, it is not all that bold(faced) and thus may not meet your needs.



              • Assuming you're willing to depart from the Computer/Latin Modern font family entirely, some good options for font families could be selected with either one of the following two commands: usepackage{pxfonts} (a Palatino font) or usepackage{txfonts} (a Times Roman font). Note that the (variable-spaced) text fonts will look very different, but the creator of these two font families gave them each the same style of monospaced text.




                • The mathptmx package, which gives you still another option for Times New Roman, does not feature a boldfaced monospaced font and hence won't do the job for you.


                • Update Jan 2019: In the meantime, i.e., since I wrote the original version of this answer back in 2011, the txfonts and pxfonts font packages have been superseded (if not entirely replaced) by newtxtext/newtxmath on the one hand and newpxtext/newpxmath on the other. By all means, if you've used the txfonts and/or pxfonts packages in the path, do consider switching to either newtxtext/newtxmath or newpxtext/newpxmath. What hasn't changed is that newtxtext and newpxtext share the same monospaced font -- both medium ("normal") and bold weight.





              The following MWE serves to illustrate the effects of choosing among these options:



              documentclass{article}
              %% Uncomment one (and only one) of the following four usepackage commands:
              %% usepackage[scaled=1.04]{couriers}
              usepackage{lmodern}
              %%usepackage{pxfonts} % Palatino font
              %%usepackage{txfonts} % Times font

              begin{document}
              %Monospaced font:
              texttt{The quick brown fox jumps over the lazy dog.}

              %Monospaced font with some boldfaced words:
              texttt{The quick brown textbf{fox} jumps over the textbf{lazy} dog.}
              end{document}


              Courier mono generates this text:



              enter image description here



              Latin Modern mono looks like this (note the rather minor difference
              between non-bold and bold):



              enter image description here



              Both the pxfonts and txfonts samples look like this:



              enter image description here






              share|improve this answer























              • Why the downvote? I wrote this answer nearly 7 years ago. If some fonts have changed since, kindly inform me what's no longer up-to-date about this answer, and I'll gladly update it. Thanks.
                – Mico
                Apr 12 '18 at 5:24










              • This does not cover italics with monospace, only bold. (I am not the one who down-voted this answer)
                – Mikaël Mayer
                Jul 30 '18 at 16:44










              • @MikaëlMayer - Thanks for this. I still would prefer for whoever saw fit to downvote this answer to provide some usable feedback regarding what irked him/her so much as to create the urge to downvote.
                – Mico
                Jul 30 '18 at 18:25
















              8














              As you must have already discovered, there is no boldfaced monospaced font in the Computer Modern font family. (Computer Modern is what's loaded by default.) To get the option of highlighting part of your code in a boldfaced font, you must switch to a different font family.




              • One way to do this is to add the command usepackage[scaled=1.04]{couriers} to the preamble of your document; doing so will tell (La)TeX to use the Courier monospaced font instead of the Computer-Modern (CM) variety. The ordinary and boldfaced versions of Courier look very different from CM mono, which is presumably what you want. The downside of using Courier is that it's set very loosely, i.e., you can't squeeze as many words into a line as you could with CM mono.


              • A fairly unobtrusive option -- in the sense that the main text and math fonts are virtually indistinguishable from Computer Modern, while having access to a monospaced font that's more compact than Courier -- would be to use the Latin Modern font family, loaded with the command usepackage{lmodern}. While this font family does have a boldface-monospaced font, it is not all that bold(faced) and thus may not meet your needs.



              • Assuming you're willing to depart from the Computer/Latin Modern font family entirely, some good options for font families could be selected with either one of the following two commands: usepackage{pxfonts} (a Palatino font) or usepackage{txfonts} (a Times Roman font). Note that the (variable-spaced) text fonts will look very different, but the creator of these two font families gave them each the same style of monospaced text.




                • The mathptmx package, which gives you still another option for Times New Roman, does not feature a boldfaced monospaced font and hence won't do the job for you.


                • Update Jan 2019: In the meantime, i.e., since I wrote the original version of this answer back in 2011, the txfonts and pxfonts font packages have been superseded (if not entirely replaced) by newtxtext/newtxmath on the one hand and newpxtext/newpxmath on the other. By all means, if you've used the txfonts and/or pxfonts packages in the path, do consider switching to either newtxtext/newtxmath or newpxtext/newpxmath. What hasn't changed is that newtxtext and newpxtext share the same monospaced font -- both medium ("normal") and bold weight.





              The following MWE serves to illustrate the effects of choosing among these options:



              documentclass{article}
              %% Uncomment one (and only one) of the following four usepackage commands:
              %% usepackage[scaled=1.04]{couriers}
              usepackage{lmodern}
              %%usepackage{pxfonts} % Palatino font
              %%usepackage{txfonts} % Times font

              begin{document}
              %Monospaced font:
              texttt{The quick brown fox jumps over the lazy dog.}

              %Monospaced font with some boldfaced words:
              texttt{The quick brown textbf{fox} jumps over the textbf{lazy} dog.}
              end{document}


              Courier mono generates this text:



              enter image description here



              Latin Modern mono looks like this (note the rather minor difference
              between non-bold and bold):



              enter image description here



              Both the pxfonts and txfonts samples look like this:



              enter image description here






              share|improve this answer























              • Why the downvote? I wrote this answer nearly 7 years ago. If some fonts have changed since, kindly inform me what's no longer up-to-date about this answer, and I'll gladly update it. Thanks.
                – Mico
                Apr 12 '18 at 5:24










              • This does not cover italics with monospace, only bold. (I am not the one who down-voted this answer)
                – Mikaël Mayer
                Jul 30 '18 at 16:44










              • @MikaëlMayer - Thanks for this. I still would prefer for whoever saw fit to downvote this answer to provide some usable feedback regarding what irked him/her so much as to create the urge to downvote.
                – Mico
                Jul 30 '18 at 18:25














              8












              8








              8






              As you must have already discovered, there is no boldfaced monospaced font in the Computer Modern font family. (Computer Modern is what's loaded by default.) To get the option of highlighting part of your code in a boldfaced font, you must switch to a different font family.




              • One way to do this is to add the command usepackage[scaled=1.04]{couriers} to the preamble of your document; doing so will tell (La)TeX to use the Courier monospaced font instead of the Computer-Modern (CM) variety. The ordinary and boldfaced versions of Courier look very different from CM mono, which is presumably what you want. The downside of using Courier is that it's set very loosely, i.e., you can't squeeze as many words into a line as you could with CM mono.


              • A fairly unobtrusive option -- in the sense that the main text and math fonts are virtually indistinguishable from Computer Modern, while having access to a monospaced font that's more compact than Courier -- would be to use the Latin Modern font family, loaded with the command usepackage{lmodern}. While this font family does have a boldface-monospaced font, it is not all that bold(faced) and thus may not meet your needs.



              • Assuming you're willing to depart from the Computer/Latin Modern font family entirely, some good options for font families could be selected with either one of the following two commands: usepackage{pxfonts} (a Palatino font) or usepackage{txfonts} (a Times Roman font). Note that the (variable-spaced) text fonts will look very different, but the creator of these two font families gave them each the same style of monospaced text.




                • The mathptmx package, which gives you still another option for Times New Roman, does not feature a boldfaced monospaced font and hence won't do the job for you.


                • Update Jan 2019: In the meantime, i.e., since I wrote the original version of this answer back in 2011, the txfonts and pxfonts font packages have been superseded (if not entirely replaced) by newtxtext/newtxmath on the one hand and newpxtext/newpxmath on the other. By all means, if you've used the txfonts and/or pxfonts packages in the path, do consider switching to either newtxtext/newtxmath or newpxtext/newpxmath. What hasn't changed is that newtxtext and newpxtext share the same monospaced font -- both medium ("normal") and bold weight.





              The following MWE serves to illustrate the effects of choosing among these options:



              documentclass{article}
              %% Uncomment one (and only one) of the following four usepackage commands:
              %% usepackage[scaled=1.04]{couriers}
              usepackage{lmodern}
              %%usepackage{pxfonts} % Palatino font
              %%usepackage{txfonts} % Times font

              begin{document}
              %Monospaced font:
              texttt{The quick brown fox jumps over the lazy dog.}

              %Monospaced font with some boldfaced words:
              texttt{The quick brown textbf{fox} jumps over the textbf{lazy} dog.}
              end{document}


              Courier mono generates this text:



              enter image description here



              Latin Modern mono looks like this (note the rather minor difference
              between non-bold and bold):



              enter image description here



              Both the pxfonts and txfonts samples look like this:



              enter image description here






              share|improve this answer














              As you must have already discovered, there is no boldfaced monospaced font in the Computer Modern font family. (Computer Modern is what's loaded by default.) To get the option of highlighting part of your code in a boldfaced font, you must switch to a different font family.




              • One way to do this is to add the command usepackage[scaled=1.04]{couriers} to the preamble of your document; doing so will tell (La)TeX to use the Courier monospaced font instead of the Computer-Modern (CM) variety. The ordinary and boldfaced versions of Courier look very different from CM mono, which is presumably what you want. The downside of using Courier is that it's set very loosely, i.e., you can't squeeze as many words into a line as you could with CM mono.


              • A fairly unobtrusive option -- in the sense that the main text and math fonts are virtually indistinguishable from Computer Modern, while having access to a monospaced font that's more compact than Courier -- would be to use the Latin Modern font family, loaded with the command usepackage{lmodern}. While this font family does have a boldface-monospaced font, it is not all that bold(faced) and thus may not meet your needs.



              • Assuming you're willing to depart from the Computer/Latin Modern font family entirely, some good options for font families could be selected with either one of the following two commands: usepackage{pxfonts} (a Palatino font) or usepackage{txfonts} (a Times Roman font). Note that the (variable-spaced) text fonts will look very different, but the creator of these two font families gave them each the same style of monospaced text.




                • The mathptmx package, which gives you still another option for Times New Roman, does not feature a boldfaced monospaced font and hence won't do the job for you.


                • Update Jan 2019: In the meantime, i.e., since I wrote the original version of this answer back in 2011, the txfonts and pxfonts font packages have been superseded (if not entirely replaced) by newtxtext/newtxmath on the one hand and newpxtext/newpxmath on the other. By all means, if you've used the txfonts and/or pxfonts packages in the path, do consider switching to either newtxtext/newtxmath or newpxtext/newpxmath. What hasn't changed is that newtxtext and newpxtext share the same monospaced font -- both medium ("normal") and bold weight.





              The following MWE serves to illustrate the effects of choosing among these options:



              documentclass{article}
              %% Uncomment one (and only one) of the following four usepackage commands:
              %% usepackage[scaled=1.04]{couriers}
              usepackage{lmodern}
              %%usepackage{pxfonts} % Palatino font
              %%usepackage{txfonts} % Times font

              begin{document}
              %Monospaced font:
              texttt{The quick brown fox jumps over the lazy dog.}

              %Monospaced font with some boldfaced words:
              texttt{The quick brown textbf{fox} jumps over the textbf{lazy} dog.}
              end{document}


              Courier mono generates this text:



              enter image description here



              Latin Modern mono looks like this (note the rather minor difference
              between non-bold and bold):



              enter image description here



              Both the pxfonts and txfonts samples look like this:



              enter image description here







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 58 mins ago

























              answered Sep 6 '11 at 14:38









              Mico

              274k30371757




              274k30371757












              • Why the downvote? I wrote this answer nearly 7 years ago. If some fonts have changed since, kindly inform me what's no longer up-to-date about this answer, and I'll gladly update it. Thanks.
                – Mico
                Apr 12 '18 at 5:24










              • This does not cover italics with monospace, only bold. (I am not the one who down-voted this answer)
                – Mikaël Mayer
                Jul 30 '18 at 16:44










              • @MikaëlMayer - Thanks for this. I still would prefer for whoever saw fit to downvote this answer to provide some usable feedback regarding what irked him/her so much as to create the urge to downvote.
                – Mico
                Jul 30 '18 at 18:25


















              • Why the downvote? I wrote this answer nearly 7 years ago. If some fonts have changed since, kindly inform me what's no longer up-to-date about this answer, and I'll gladly update it. Thanks.
                – Mico
                Apr 12 '18 at 5:24










              • This does not cover italics with monospace, only bold. (I am not the one who down-voted this answer)
                – Mikaël Mayer
                Jul 30 '18 at 16:44










              • @MikaëlMayer - Thanks for this. I still would prefer for whoever saw fit to downvote this answer to provide some usable feedback regarding what irked him/her so much as to create the urge to downvote.
                – Mico
                Jul 30 '18 at 18:25
















              Why the downvote? I wrote this answer nearly 7 years ago. If some fonts have changed since, kindly inform me what's no longer up-to-date about this answer, and I'll gladly update it. Thanks.
              – Mico
              Apr 12 '18 at 5:24




              Why the downvote? I wrote this answer nearly 7 years ago. If some fonts have changed since, kindly inform me what's no longer up-to-date about this answer, and I'll gladly update it. Thanks.
              – Mico
              Apr 12 '18 at 5:24












              This does not cover italics with monospace, only bold. (I am not the one who down-voted this answer)
              – Mikaël Mayer
              Jul 30 '18 at 16:44




              This does not cover italics with monospace, only bold. (I am not the one who down-voted this answer)
              – Mikaël Mayer
              Jul 30 '18 at 16:44












              @MikaëlMayer - Thanks for this. I still would prefer for whoever saw fit to downvote this answer to provide some usable feedback regarding what irked him/her so much as to create the urge to downvote.
              – Mico
              Jul 30 '18 at 18:25




              @MikaëlMayer - Thanks for this. I still would prefer for whoever saw fit to downvote this answer to provide some usable feedback regarding what irked him/her so much as to create the urge to downvote.
              – Mico
              Jul 30 '18 at 18:25











              6














              you need a typewriter font which supports bold characters, eg:



              usepackage[scale=0.9]{beramono}


              and inside the listing you can use the escape character. You'l lfind some examples in the documentation, run texdoc listings






              share|improve this answer

















              • 1




                Or you could usepackage{bold-extra} if you want a bold version of cmtt.
                – kahen
                Nov 27 '11 at 21:28










              • no, I don't want the cmtt font ...
                – Herbert
                Nov 27 '11 at 21:38
















              6














              you need a typewriter font which supports bold characters, eg:



              usepackage[scale=0.9]{beramono}


              and inside the listing you can use the escape character. You'l lfind some examples in the documentation, run texdoc listings






              share|improve this answer

















              • 1




                Or you could usepackage{bold-extra} if you want a bold version of cmtt.
                – kahen
                Nov 27 '11 at 21:28










              • no, I don't want the cmtt font ...
                – Herbert
                Nov 27 '11 at 21:38














              6












              6








              6






              you need a typewriter font which supports bold characters, eg:



              usepackage[scale=0.9]{beramono}


              and inside the listing you can use the escape character. You'l lfind some examples in the documentation, run texdoc listings






              share|improve this answer












              you need a typewriter font which supports bold characters, eg:



              usepackage[scale=0.9]{beramono}


              and inside the listing you can use the escape character. You'l lfind some examples in the documentation, run texdoc listings







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Sep 6 '11 at 12:55









              Herbert

              270k24408717




              270k24408717








              • 1




                Or you could usepackage{bold-extra} if you want a bold version of cmtt.
                – kahen
                Nov 27 '11 at 21:28










              • no, I don't want the cmtt font ...
                – Herbert
                Nov 27 '11 at 21:38














              • 1




                Or you could usepackage{bold-extra} if you want a bold version of cmtt.
                – kahen
                Nov 27 '11 at 21:28










              • no, I don't want the cmtt font ...
                – Herbert
                Nov 27 '11 at 21:38








              1




              1




              Or you could usepackage{bold-extra} if you want a bold version of cmtt.
              – kahen
              Nov 27 '11 at 21:28




              Or you could usepackage{bold-extra} if you want a bold version of cmtt.
              – kahen
              Nov 27 '11 at 21:28












              no, I don't want the cmtt font ...
              – Herbert
              Nov 27 '11 at 21:38




              no, I don't want the cmtt font ...
              – Herbert
              Nov 27 '11 at 21:38











              2














              The listings package might suffice.



              Start with:



              usepackage{listings}

              ...

              lstset{morekeywords={example,foo,bar}}

              ...

              begin[language=C]{lstlisting}
              YOUR_CODE_HERE
              end{lstlisting}


              The package allows you to define your own language definition (lstdefinelanguage), and you get some fine-grained control about how the language is presented, so you get some re-usability of your setup (i.e., preferable over manually setting your keywords bold).






              share|improve this answer





















              • Does the listings package provide the option of bold-facing (if desired) complete sentences at a time? Also, can you control when words such as "example", "foo", and "bar" should be typeset in boldface-mono and when they should be printed in the normal mono font?
                – Mico
                Sep 6 '11 at 14:43










              • Beyond my knowledge of the package. Since I do not know what exactly your requirements are (and would have to look up the package documentation myself), I'd recommend you sift through the docs at the link above.
                – DevSolar
                Sep 6 '11 at 16:21










              • I am actually not the person who originally posted the question, and hence I don't either know what his/her specific needs (in terms of bold-facing some words) might be. It just struck me that the morekeywords listing facility might not be flexible enough to meet his/her needs.
                – Mico
                Sep 6 '11 at 18:14










              • Chapter 4.14 Escaping to LaTeX of the listings package documentation provides several ways to embed LaTeX commands in your listing.
                – DevSolar
                Sep 7 '11 at 14:34
















              2














              The listings package might suffice.



              Start with:



              usepackage{listings}

              ...

              lstset{morekeywords={example,foo,bar}}

              ...

              begin[language=C]{lstlisting}
              YOUR_CODE_HERE
              end{lstlisting}


              The package allows you to define your own language definition (lstdefinelanguage), and you get some fine-grained control about how the language is presented, so you get some re-usability of your setup (i.e., preferable over manually setting your keywords bold).






              share|improve this answer





















              • Does the listings package provide the option of bold-facing (if desired) complete sentences at a time? Also, can you control when words such as "example", "foo", and "bar" should be typeset in boldface-mono and when they should be printed in the normal mono font?
                – Mico
                Sep 6 '11 at 14:43










              • Beyond my knowledge of the package. Since I do not know what exactly your requirements are (and would have to look up the package documentation myself), I'd recommend you sift through the docs at the link above.
                – DevSolar
                Sep 6 '11 at 16:21










              • I am actually not the person who originally posted the question, and hence I don't either know what his/her specific needs (in terms of bold-facing some words) might be. It just struck me that the morekeywords listing facility might not be flexible enough to meet his/her needs.
                – Mico
                Sep 6 '11 at 18:14










              • Chapter 4.14 Escaping to LaTeX of the listings package documentation provides several ways to embed LaTeX commands in your listing.
                – DevSolar
                Sep 7 '11 at 14:34














              2












              2








              2






              The listings package might suffice.



              Start with:



              usepackage{listings}

              ...

              lstset{morekeywords={example,foo,bar}}

              ...

              begin[language=C]{lstlisting}
              YOUR_CODE_HERE
              end{lstlisting}


              The package allows you to define your own language definition (lstdefinelanguage), and you get some fine-grained control about how the language is presented, so you get some re-usability of your setup (i.e., preferable over manually setting your keywords bold).






              share|improve this answer












              The listings package might suffice.



              Start with:



              usepackage{listings}

              ...

              lstset{morekeywords={example,foo,bar}}

              ...

              begin[language=C]{lstlisting}
              YOUR_CODE_HERE
              end{lstlisting}


              The package allows you to define your own language definition (lstdefinelanguage), and you get some fine-grained control about how the language is presented, so you get some re-usability of your setup (i.e., preferable over manually setting your keywords bold).







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Sep 6 '11 at 12:56









              DevSolar

              4,29983660




              4,29983660












              • Does the listings package provide the option of bold-facing (if desired) complete sentences at a time? Also, can you control when words such as "example", "foo", and "bar" should be typeset in boldface-mono and when they should be printed in the normal mono font?
                – Mico
                Sep 6 '11 at 14:43










              • Beyond my knowledge of the package. Since I do not know what exactly your requirements are (and would have to look up the package documentation myself), I'd recommend you sift through the docs at the link above.
                – DevSolar
                Sep 6 '11 at 16:21










              • I am actually not the person who originally posted the question, and hence I don't either know what his/her specific needs (in terms of bold-facing some words) might be. It just struck me that the morekeywords listing facility might not be flexible enough to meet his/her needs.
                – Mico
                Sep 6 '11 at 18:14










              • Chapter 4.14 Escaping to LaTeX of the listings package documentation provides several ways to embed LaTeX commands in your listing.
                – DevSolar
                Sep 7 '11 at 14:34


















              • Does the listings package provide the option of bold-facing (if desired) complete sentences at a time? Also, can you control when words such as "example", "foo", and "bar" should be typeset in boldface-mono and when they should be printed in the normal mono font?
                – Mico
                Sep 6 '11 at 14:43










              • Beyond my knowledge of the package. Since I do not know what exactly your requirements are (and would have to look up the package documentation myself), I'd recommend you sift through the docs at the link above.
                – DevSolar
                Sep 6 '11 at 16:21










              • I am actually not the person who originally posted the question, and hence I don't either know what his/her specific needs (in terms of bold-facing some words) might be. It just struck me that the morekeywords listing facility might not be flexible enough to meet his/her needs.
                – Mico
                Sep 6 '11 at 18:14










              • Chapter 4.14 Escaping to LaTeX of the listings package documentation provides several ways to embed LaTeX commands in your listing.
                – DevSolar
                Sep 7 '11 at 14:34
















              Does the listings package provide the option of bold-facing (if desired) complete sentences at a time? Also, can you control when words such as "example", "foo", and "bar" should be typeset in boldface-mono and when they should be printed in the normal mono font?
              – Mico
              Sep 6 '11 at 14:43




              Does the listings package provide the option of bold-facing (if desired) complete sentences at a time? Also, can you control when words such as "example", "foo", and "bar" should be typeset in boldface-mono and when they should be printed in the normal mono font?
              – Mico
              Sep 6 '11 at 14:43












              Beyond my knowledge of the package. Since I do not know what exactly your requirements are (and would have to look up the package documentation myself), I'd recommend you sift through the docs at the link above.
              – DevSolar
              Sep 6 '11 at 16:21




              Beyond my knowledge of the package. Since I do not know what exactly your requirements are (and would have to look up the package documentation myself), I'd recommend you sift through the docs at the link above.
              – DevSolar
              Sep 6 '11 at 16:21












              I am actually not the person who originally posted the question, and hence I don't either know what his/her specific needs (in terms of bold-facing some words) might be. It just struck me that the morekeywords listing facility might not be flexible enough to meet his/her needs.
              – Mico
              Sep 6 '11 at 18:14




              I am actually not the person who originally posted the question, and hence I don't either know what his/her specific needs (in terms of bold-facing some words) might be. It just struck me that the morekeywords listing facility might not be flexible enough to meet his/her needs.
              – Mico
              Sep 6 '11 at 18:14












              Chapter 4.14 Escaping to LaTeX of the listings package documentation provides several ways to embed LaTeX commands in your listing.
              – DevSolar
              Sep 7 '11 at 14:34




              Chapter 4.14 Escaping to LaTeX of the listings package documentation provides several ways to embed LaTeX commands in your listing.
              – DevSolar
              Sep 7 '11 at 14:34











              2














              I can't remember where I came across the following definition to use a boldface typewriter font with Computer Modern:



              % Declare bold typewriter font with Computer Modern
              DeclareFontShape{OT1}{cmtt}{bx}{n}{<5><6><7><8><9><10><10.95><12><14.4><17.28><20.74><24.88>cmttb10}{}


              Then:



              usepackage{listings}
              lstloadlanguages{Matlab}
              lstset{
              basicstyle=scriptsizeupshapettfamily,
              keywordstyle=color{blue}bfseries,
              % etc.
              }


              Result:



              enter image description here



              I'm not an expert in fonts in LaTeX so I'm not entirely sure what the DeclareFontShape command does behind the scenes but I like the result.






              share|improve this answer


























                2














                I can't remember where I came across the following definition to use a boldface typewriter font with Computer Modern:



                % Declare bold typewriter font with Computer Modern
                DeclareFontShape{OT1}{cmtt}{bx}{n}{<5><6><7><8><9><10><10.95><12><14.4><17.28><20.74><24.88>cmttb10}{}


                Then:



                usepackage{listings}
                lstloadlanguages{Matlab}
                lstset{
                basicstyle=scriptsizeupshapettfamily,
                keywordstyle=color{blue}bfseries,
                % etc.
                }


                Result:



                enter image description here



                I'm not an expert in fonts in LaTeX so I'm not entirely sure what the DeclareFontShape command does behind the scenes but I like the result.






                share|improve this answer
























                  2












                  2








                  2






                  I can't remember where I came across the following definition to use a boldface typewriter font with Computer Modern:



                  % Declare bold typewriter font with Computer Modern
                  DeclareFontShape{OT1}{cmtt}{bx}{n}{<5><6><7><8><9><10><10.95><12><14.4><17.28><20.74><24.88>cmttb10}{}


                  Then:



                  usepackage{listings}
                  lstloadlanguages{Matlab}
                  lstset{
                  basicstyle=scriptsizeupshapettfamily,
                  keywordstyle=color{blue}bfseries,
                  % etc.
                  }


                  Result:



                  enter image description here



                  I'm not an expert in fonts in LaTeX so I'm not entirely sure what the DeclareFontShape command does behind the scenes but I like the result.






                  share|improve this answer












                  I can't remember where I came across the following definition to use a boldface typewriter font with Computer Modern:



                  % Declare bold typewriter font with Computer Modern
                  DeclareFontShape{OT1}{cmtt}{bx}{n}{<5><6><7><8><9><10><10.95><12><14.4><17.28><20.74><24.88>cmttb10}{}


                  Then:



                  usepackage{listings}
                  lstloadlanguages{Matlab}
                  lstset{
                  basicstyle=scriptsizeupshapettfamily,
                  keywordstyle=color{blue}bfseries,
                  % etc.
                  }


                  Result:



                  enter image description here



                  I'm not an expert in fonts in LaTeX so I'm not entirely sure what the DeclareFontShape command does behind the scenes but I like the result.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 14 '13 at 22:34









                  Dominique

                  787513




                  787513






























                      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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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%2f27663%2fusing-bold-italic-text-inside-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

                      Lallio

                      Futebolista

                      Jornalista