{"id":1961,"date":"2015-11-07T11:48:21","date_gmt":"2015-11-07T10:48:21","guid":{"rendered":"http:\/\/denbeke.be\/blog\/?p=1961"},"modified":"2015-11-09T17:58:07","modified_gmt":"2015-11-09T16:58:07","slug":"syntax-highlighting-in-latex","status":"publish","type":"post","link":"https:\/\/denbeke.be\/blog\/programming\/syntax-highlighting-in-latex\/","title":{"rendered":"Syntax highlighting in LaTeX with lstlisting"},"content":{"rendered":"<p>When adding code snippets to my LaTeX\u00a0documents, I use <code>lstlisting<\/code>. It is a syntax highlighting environment that can easily be tweaked to match your own styling.<\/p>\n<figure id=\"attachment_1966\" aria-describedby=\"caption-attachment-1966\" style=\"width: 642px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/denbeke.be\/blog\/programming\/syntax-highlighting-in-latex\/attachment\/latex-listlisting-syntax-highlighting-example\/\" rel=\"attachment wp-att-1966\"><img loading=\"lazy\" class=\"wp-image-1966 \" src=\"http:\/\/denbeke.be\/blog\/wp-content\/uploads\/2015\/11\/LaTex-listlisting-syntax-highlighting-example.png\" alt=\"LaTex listlisting syntax highlighting example\" width=\"642\" height=\"134\" srcset=\"https:\/\/denbeke.be\/blog\/wp-content\/uploads\/2015\/11\/LaTex-listlisting-syntax-highlighting-example.png 1001w, https:\/\/denbeke.be\/blog\/wp-content\/uploads\/2015\/11\/LaTex-listlisting-syntax-highlighting-example-300x63.png 300w\" sizes=\"(max-width: 642px) 100vw, 642px\" \/><\/a><figcaption id=\"caption-attachment-1966\" class=\"wp-caption-text\">Example of syntax highlighting with <code>lstlisting<\/code> and the style defined below.<\/figcaption><\/figure>\n<p>To do this you need to use the package <code>listings<\/code>:\u00a0<code>\\usepackage{listings}<\/code>.<\/p>\n<p>Code snippets are added like this:<\/p>\n<pre><code class=\"tex\">\\begin{lstlisting}\r\nSome super cool code snippet...\r\n\\end{lstlisting}<\/code><\/pre>\n<p>Adding a code snippet from file is also possible:<\/p>\n<pre><code class=\"tex\">\\lstinputlisting{source_filename.py}<\/code><\/pre>\n<p>You can changes the styling of the code fragments (making keywords bold, comments italic, changing colors, &#8230;) by specifying <code>lstset<\/code>:<\/p>\n<pre><code class=\"tex\">\\lstset{ %\r\n  backgroundcolor=\\color{white},   % choose the background color; you must add \\usepackage{color} or \\usepackage{xcolor}\r\n  basicstyle=\\footnotesize,        % the size of the fonts that are used for the code\r\n  breakatwhitespace=false,         % sets if automatic breaks should only happen at whitespace\r\n  breaklines=true,                 % sets automatic line breaking\r\n  captionpos=b,                    % sets the caption-position to bottom\r\n  commentstyle=\\color{commentsColor}\\textit,    % comment style\r\n  deletekeywords={...},            % if you want to delete keywords from the given language\r\n  escapeinside={\\%*}{*)},          % if you want to add LaTeX within your code\r\n  extendedchars=true,              % lets you use non-ASCII characters; for 8-bits encodings only, does not work with UTF-8\r\n  frame=tb,\t                   \t   % adds a frame around the code\r\n  keepspaces=true,                 % keeps spaces in text, useful for keeping indentation of code (possibly needs columns=flexible)\r\n  keywordstyle=\\color{keywordsColor}\\bfseries,       % keyword style\r\n  language=Python,                 % the language of the code (can be overrided per snippet)\r\n  otherkeywords={*,...},           % if you want to add more keywords to the set\r\n  numbers=left,                    % where to put the line-numbers; possible values are (none, left, right)\r\n  numbersep=5pt,                   % how far the line-numbers are from the code\r\n  numberstyle=\\tiny\\color{commentsColor}, % the style that is used for the line-numbers\r\n  rulecolor=\\color{black},         % if not set, the frame-color may be changed on line-breaks within not-black text (e.g. comments (green here))\r\n  showspaces=false,                % show spaces everywhere adding particular underscores; it overrides 'showstringspaces'\r\n  showstringspaces=false,          % underline spaces within strings only\r\n  showtabs=false,                  % show tabs within strings adding particular underscores\r\n  stepnumber=1,                    % the step between two line-numbers. If it's 1, each line will be numbered\r\n  stringstyle=\\color{stringColor}, % string literal style\r\n  tabsize=2,\t                   % sets default tabsize to 2 spaces\r\n  title=\\lstname,                  % show the filename of files included with \\lstinputlisting; also try caption instead of title\r\n  columns=fixed                    % Using fixed column width (for e.g. nice alignment)\r\n}\r\n<\/code><\/pre>\n<p>When you use your own colors, you must naturally define them:<\/p>\n<pre><code class=\"tex\">\\usepackage{xcolor}\r\n\r\n\\definecolor{commentsColor}{rgb}{0.497495, 0.497587, 0.497464}\r\n\\definecolor{keywordsColor}{rgb}{0.000000, 0.000000, 0.635294}\r\n\\definecolor{stringColor}{rgb}{0.558215, 0.000000, 0.135316}<\/code><\/pre>\n<p>If needed you can also specify your own language syntax:<\/p>\n<pre><code class=\"tex\">\\lstdefinelanguage{JavaScript}{\r\n  keywords={typeof, new, true, false, catch, function, return, null, catch, switch, var, if, in, while, do, else, case, break},\r\n  keywordstyle=\\color{blue}\\bfseries,\r\n  ndkeywords={class, export, boolean, throw, implements, import, this},\r\n  ndkeywordstyle=\\color{darkgray}\\bfseries,\r\n  identifierstyle=\\color{black},\r\n  sensitive=false,\r\n  comment=[l]{\/\/},\r\n  morecomment=[s]{\/*}{*\/},\r\n  commentstyle=\\color{purple}\\ttfamily,\r\n  stringstyle=\\color{red}\\ttfamily,\r\n  morestring=[b]',\r\n  morestring=[b]\"\r\n}\r\n<\/code><\/pre>\n<p>More info can be found in the LaTeX documentation: <a href=\"https:\/\/en.wikibooks.org\/wiki\/LaTeX\/Source_Code_Listings\" target=\"_blank\">Source Code Listings<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When adding code snippets to my LaTeX\u00a0documents, I use lstlisting. It is a syntax highlighting environment that can easily be tweaked to match your own styling. To do this you need to use the package listings:\u00a0\\usepackage{listings}. Code snippets are added like this: \\begin{lstlisting} Some super cool code snippet&#8230; \\end{lstlisting} Adding a code snippet from file [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[235],"tags":[229],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v15.6.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Syntax highlighting in LaTeX with lstlisting &ndash; DenBeke<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/denbeke.be\/blog\/programming\/syntax-highlighting-in-latex\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Syntax highlighting in LaTeX with lstlisting &ndash; DenBeke\" \/>\n<meta property=\"og:description\" content=\"When adding code snippets to my LaTeX\u00a0documents, I use lstlisting. It is a syntax highlighting environment that can easily be tweaked to match your own styling. To do this you need to use the package listings:\u00a0\\usepackage{listings}. Code snippets are added like this: \\begin{lstlisting} Some super cool code snippet... \\end{lstlisting} Adding a code snippet from file [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/denbeke.be\/blog\/programming\/syntax-highlighting-in-latex\/\" \/>\n<meta property=\"og:site_name\" content=\"DenBeke\" \/>\n<meta property=\"article:published_time\" content=\"2015-11-07T10:48:21+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2015-11-09T16:58:07+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/denbeke.be\/blog\/wp-content\/uploads\/2015\/11\/LaTex-listlisting-syntax-highlighting-example.png\" \/>\n<meta name=\"twitter:card\" content=\"summary\" \/>\n<meta name=\"twitter:creator\" content=\"@MthsBk\" \/>\n<meta name=\"twitter:site\" content=\"@MthsBk\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\">\n\t<meta name=\"twitter:data1\" content=\"2 minutes\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/denbeke.be\/blog\/#website\",\"url\":\"https:\/\/denbeke.be\/blog\/\",\"name\":\"DenBeke\",\"description\":\"Mathias Beke\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/denbeke.be\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/denbeke.be\/blog\/programming\/syntax-highlighting-in-latex\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"http:\/\/denbeke.be\/blog\/wp-content\/uploads\/2015\/11\/LaTex-listlisting-syntax-highlighting-example.png\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/denbeke.be\/blog\/programming\/syntax-highlighting-in-latex\/#webpage\",\"url\":\"https:\/\/denbeke.be\/blog\/programming\/syntax-highlighting-in-latex\/\",\"name\":\"Syntax highlighting in LaTeX with lstlisting &ndash; DenBeke\",\"isPartOf\":{\"@id\":\"https:\/\/denbeke.be\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/denbeke.be\/blog\/programming\/syntax-highlighting-in-latex\/#primaryimage\"},\"datePublished\":\"2015-11-07T10:48:21+00:00\",\"dateModified\":\"2015-11-09T16:58:07+00:00\",\"author\":{\"@id\":\"https:\/\/denbeke.be\/blog\/#\/schema\/person\/386878f712fe3fe22227216f087772dc\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/denbeke.be\/blog\/programming\/syntax-highlighting-in-latex\/\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/denbeke.be\/blog\/#\/schema\/person\/386878f712fe3fe22227216f087772dc\",\"name\":\"Mathias Beke\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/denbeke.be\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/015ba35e6ce4f5859e3888ca99807575?s=96&d=mm&r=g\",\"caption\":\"Mathias Beke\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts\/1961"}],"collection":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/comments?post=1961"}],"version-history":[{"count":14,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts\/1961\/revisions"}],"predecessor-version":[{"id":1976,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts\/1961\/revisions\/1976"}],"wp:attachment":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/media?parent=1961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/categories?post=1961"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/tags?post=1961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}