New answers tagged

2

You need to add the command \selectfont to "activate" the line spacing changes: \lstset{ basicstyle=\ttm\linespread{0.8}\selectfont } If you dislike the extra spacing below the listing, you can also set belowskip=0pt.


1

When listings breaks a line that is too long, it inserts some space at the beginning of the new line(s). You can remove this space by setting breakindent=0pt in \lstset: \documentclass{article} \usepackage[T1]{fontenc} \usepackage{listings} \title{Example} \lstset{ basicstyle=\normalsize\ttfamily, breaklines=true, framextopmargin=10pt, ...


1

Here's a solution that makes use of the general number highlighting approach presented in Listings: color numbers only out of keywords. We need to make some changes to the OutputOther hook from that solution, because in this case you want to override the default style no matter whether you are in a comment or not. The new hook looks like: \lst@AddToHook{...


4

You can set the margin with tcolorbox keys: \documentclass[11pt]{beamer} \usepackage{tcolorbox} \tcbuselibrary{listings,skins} \lstdefinestyle{compactstyle}{ numbers=left, numberstyle=\small, numbersep=8pt, language=Python, basicstyle=\tiny, } \newtcblisting{compactlisting}[2][]{ arc=0pt, outer arc=0pt, listing only, listing ...


3

You can wrap the code listing in a figure environment as demonstrated below. \documentclass{article} \usepackage{listings} \begin{document} \begin{figure}[hbtp] \caption{Code} \label{code.1} \begin{lstlisting}[language=Mathematica] f1 = Cos[2*Pi*freq1*t]; freq = 1 + 2*t; f2 = Cos[2*Pi*Integrate[freq, t]*t]; \end{...


2

The Matlab language definition only defines ' as string delimiter by default. But you can also make " one by adding the following line to the listing's options: morestring=[m]" m is a special delimiter type for Matlab code defined as [...] a special type for Ada and Matlab and possibly other languages where the string delimiters are also used for ...


7

Just avoid passing the argument and define \newcommand{\cd}{\lstinline}


2

The label change can be done, e.g., by loading the package with required square symbol and applying this symbol. The package name is bbding and the symbol name \SquareCastShadowBottomRight (see the code snippet below marked by Q1). But there is a conflict with the package marvosym used in your code (the \Cross command is defined in both packages). Therefore, ...


1

\documentclass{article} \usepackage{xcolor} \usepackage{listings} \usepackage[many]{tcolorbox} \tcbuselibrary{listings} \definecolor{verylightgray}{gray}{0.9} \newtcblisting{graylstlisting}[1][] { spartan, frame empty, boxsep=0mm, listing only, colback=verylightgray, top = 0pt, %<- bottom = 0pt,%<- left=0pt,%<- listing options=...


1

There were multiple problems in this code: We do not want to escape any character, if used as an input and, as @David Carlisle mentioned, the input for lsinputlistings needs to be expanded. The optional structure is, as @egreg mentioned, better delegated to xparse. For getting an optional command I used the DeclareExpandableDocumentCommand from (i think) ...


1

You cannot have page breaks in a figure environment, so don't place the listing in a figure. The lstlisting environment has a caption parameter that you can use to add the caption. Regarding the frame, you first have frame=single, then you have a second \lstset with frame=bottomrule, meaning you only get a line at end of the listing. Remove the second \...


Top 50 recent answers are included