Writing code in a line without starting new line
Asked Answered
E

5

5

When I write code in platex using {lstlisting}

\begin{lstlisting}
if True:
   print("latex")
\end{lstlisting}

However how can I put the code in the sentence??

like the ` in stackoverflow.

I want to show like below

"Please look at this code print("latex") ,this code is nice"

Endocarp answered 8/2, 2021 at 16:2 Comment(1)
there is an inline listings macro. Can you make a compilable minimal reproducible example?Mahdi
M
10

You can use the \lstinline macro:

\documentclass{article}

\usepackage{listings}
\lstset{basicstyle=\ttfamily}

\begin{document}


test \lstinline|code| test

\end{document}

enter image description here

Mahdi answered 8/2, 2021 at 16:21 Comment(0)
R
7

Also, the core macro verb:

\documentclass{article}

\begin{document}

Text text text \verb|code \ @ } code| text text.

\end{document}

Not only braces and vertical bars | can delimit the content or argument here (see the comments below) but any other character, as long as it is the same before and after and not part of the the inline code. For example \verb!code \ @ } code! will work just as well.

As you can see, special characters need no escape within verb.

Remorseless answered 9/2, 2021 at 8:58 Comment(4)
\verb is a core macro, you don't need the verbatim package for itMahdi
Instead of the vertical bars, curly parenthesis are also possible or just about any other character as long as it is the same before and after and not part of the the inline codeMahdi
For example \verb!code \ @ } code! will work just as well as |Mahdi
Thank you @Mahdi for clarifying this particular fact!!Remorseless
M
0

The minted package also has an option for inline code:

\documentclass{article}

\usepackage{minted}

\begin{document}

test \mintinline{c++}|code| test

\end{document}

enter image description here

(make sure you have minted v3.0.0 or newer, otherwise you could need to compile with shell-escape enabled)

Mahdi answered 5/10, 2024 at 14:33 Comment(0)
M
0

For inline code with a coloured background, I would suggest the tcolorbox package:

\documentclass{article}

\usepackage[most]{tcolorbox}

\tcbset{
  frame empty,
  colback=lightgray!60,
  sharp corners,
  size=fbox
}

\begin{document}

test \tcboxverb{code} test

\end{document}

enter image description here

Mahdi answered 5/10, 2024 at 14:38 Comment(2)
That looks really nice, thanks! How can I define a command \code{} that makes use of this? \newcommand{\code}[1]{\tcboxverb{#1}} errors that the verbatim-like command \tcboxverb is illegal in the argument.Aqualung
@Aqualung If you have a new question, please ask a new questionMahdi
A
-1

Use \texttt{} to set the text to the "teletype" fixed-width font.
See here

Alluvial answered 25/9, 2024 at 8:32 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.