New answers tagged

4

You need to use a different delimiter to denote the start-and-end of your code snippet, similar to what is requested when using \verb: \documentclass{article} \usepackage{listings} \lstset{basicstyle=\ttfamily} \begin{document} Here is some Java: \lstinline!{{#if}} ... {{else}} ... {{/if}}! \end{document} Above the delimiter ! denotes the ...


1

A straight-forward way of doing this would be to use listings' capability to perform literate replacements: \documentclass{article} \usepackage{listings,xcolor} \colorlet{numbercol}{blue}% Colour of numbers: 0-9 and A-F \colorlet{keywordcol}{red}% Colour of keywords: G-Z and ,.<>;: \colorlet{stringcol}{green}% Colour of strings: "..." \lstset{ ...


2

This works: Compile twice and the reference is showing up correctly. Use the label= key of the \lstinputlisting command to provide the label name, not something like \caption...\label. \documentclass{article} \usepackage[utf8]{inputenc} \usepackage[ngerman]{babel} \usepackage{listings} \begin{document} Im Quellcodausschnitt~\ref{lst:product_xml} ...


3

looks like a bug but as usual with these things, an extra {} helps \documentclass[11pt]{article} \usepackage{listings} \usepackage{matlab-prettifier} \lstdefinestyle{matlab}{style=Matlab-editor, basicstyle=\ttfamily\normalsize, escapechar = `, mlshowsectionrules = true, frame=none } \begin{document} \begin{tabular}{p{0.5\textwidth} } ...


1

Here's a proposition with tcblisting stuff, which is very powerful, but I restricted to the basic usage -- it can be easily extended with a lot of options. Be sure to use the listing only option to the environment, otherwise tcolorbox tries to typeset both code and its LaTeX output. This must fail with of course with things like \documentclass, ...


1

Here is the trick used in @jkuczm 's link: you have to define a new command: \newcommand{\functionDefHighlight}[1]{\textcolor{identifier}{function} \textcolor{normal}{ #1}(} and then use it when parametring moredelim: moredelim = [is][\functionDefHighlight]{function\ }{(},


1

The acmsmall class only knows figure and table so it assumes that, if a float is not figure, it is table. You can reverse the logic whereby the \@makecaption selects the caption formatting styles, so it only applies “short” text width for tables. \documentclass {acmsmall} % Aptara syntax \usepackage{listings} \usepackage{etoolbox} \makeatletter ...


4

Reading Bash lstlistings treats “$#” as a comment gave me the idea to just add a literate to solve this case. In my listings settings, I added literate={\$\{}{{{{\bfseries{}\$\{}}}}2, and removed $\{ from the otherkeywords list entirely. In point of fact, unlike my MWE, since I am using LyX, I solved it by adding it to an \AtBeginDocument{} in my LaTeX ...


4

Environment macrocode does a lot more than just being a verbatim environment. Replacing it by lstlisting is not trivial: The indexing of macro names is lost. Problematic is the last line with the end tokens (\end{macrocode} or \end{lstlisting}, because of the percent char in the first column. The formatting of the docstrip tags is different. Also the ...


1

use the optional argument float: \lstinputlisting[language=php,caption={PHP code snippet A}, float, label=listing:codeA.php]{./codeA.php}


2

You can use \clearpage before the listing to flush any floats.


2

The subdepth package changes some \fontdimen parameters for the math fonts and it has to do it when those fonts are first loaded. In your case, the first loading happens during initialization of lstlisting and the combination between this step, the needed setup for subdepth and the presence of breqn that changes the meaning of all characters in math, breaks. ...


5

Perhaps escaping to LaTeX could be an option. \documentclass{article} \usepackage{listings} \begin{document} \begin{lstlisting}[language=sh,escapechar=@] cat foo.c cat.c bar.c echo The @cat@ ate the cheese \end{lstlisting} \end{document}


2

From the manual page 34: caption={[⟨short⟩]⟨caption text⟩} The caption is made of \lstlistingname followed by a running number, a seperator, and ⟨caption text⟩. Either the caption text or, if present, ⟨short⟩ will be used for the list of listings. So in your case: \begin{lstlisting}[caption={[short caption]{long caption}}] <code> ...


3

The caption package cooperates with listings, using \captionsetup[lstlistings]{…}: \documentclass{article} \usepackage{caption} \captionsetup[lstlisting]{labelsep=none, singlelinecheck=off} \usepackage{hyperref} \usepackage{listings} \begin{document} \autoref{lst:some_code}. \begin{lstlisting}[float,frame=single, caption={\empty}, label={lst:some_code}] ...


2

Another simple example to artificially gobble leading spaces (or tabs) in a \begin{tcblisting} block. That is, based on @ThomasFSturm's tcolorbox package and his above example. \documentclass[12pt,a4paper]{article} \usepackage[listings]{tcolorbox} % Uncomment to reverse the default order. %\tcbset{text and listing} % tabsize: increasing this increases ...


2

A crude workaround is the redefinition of the symbols using the literate feature. It comes with some limitations: Every upright symbol needs to be replaced. Symbols inside keywords, ... are not recognized. Italic correction is not applied between slanted and upright text. Example: \documentclass{article} \usepackage[english]{babel} ...


1

Keywords goes until the first "non-digit or non-letter". So you need to make the number to other: \documentclass[ 12pt ]{scrreprt} \usepackage{listings} \usepackage{color} \begin{document} \lstset{ morekeywords={E, T, G1, F, X, Y}, alsoother={0,1,2,3,4,5,6,7,8,9} } \begin{lstlisting} T0 G1 E2 F80 G1 Z0.400 F7800.00 G1 F300.000 E-1.00000 G1 ...


1

Hungarian version based on previous answer: \lstset{ literate={á}{{\'a}}1 {é}{{\'e}}1 {í}{{{\'\i}}}1 {ó}{{\'o}}1 {ö}{{\"o}}1 {ő}{{\H o}}1 {ú}{{\'u}}1 {Ú}{{\'U}}1 {ü}{{\"u}}1 {ű}{{\H u}}1 {Ü}{{\"U}}1 }


1

There seems to be an incompatibility between xelatex and tcolorbox regarding colors--and perhaps also listings. It is currently unclear which one is at fault. Temporary Solution Explicitly set the color after each \end{myexample} by adding \AfterEndEnvironment{myexample}{\color{black}} to the preamble. This should be safe because xcolor explicitly sets ...


4

There's some strange interaction between listings wrapped in a tcolorbox and xelatex. I recommend to use a special \newtcblisting and use this (in the outer wrapper, if really needed ;-)) In this case the text does not vanish, regardless whether it is compiled with pdflatex, xelatex or lualatex. \documentclass{article} \usepackage{fontspec}%compile with ...


2

You can tweak the corners of the box overlay either with xshift and yshift or with the calc tikzlibrary by adding a coordinate tuple to the corner identifiers (here frame.north west and frame.south west). MWE: \documentclass{article} \usepackage{listings} \usepackage[many]{tcolorbox} \usetikzlibrary{calc} \tcbuselibrary{listings} \newtcblisting[auto ...


4

See the improved version at the bottom, using tikz styles. coltitle=black works wonders (the default is coltitle=white) ;-), but white text colour on white background may be difficult to read (;-)) To use a common counter for both listings environmens, use use counter from=cpp, for example. \documentclass{book} \usepackage{listings} ...


3

In case that tocloft.sty is not a must, I can offer a solution with titletoc.sty. I think the interface is cleaner. There is also a solution at the end how to get rid of the additional vertical space in LOF. \documentclass[12pt,a4paper,draft,openany]{book} % For french \usepackage[T1]{fontenc} \usepackage[utf8]{inputenc} \usepackage[french]{babel} % For ...


4

By default tocloft can't hook into ToCs that are made by other packages, however, it's possible to use \newlistof{...} to redefine the \lstoflistings -- unfortunately, this requires knowledge of the counter being used for listings (its name is lstlisting) and \newlistof... tries to define a new counter for this -- this must fail (and is a design flaw of ...



Top 50 recent answers are included