Writing code in a line without starting new line - latex

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"

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.

You can use the \lstinline macro:
\documentclass{jarticle}
\usepackage{amsmath,amsfonts}
\usepackage{listings}
\begin{document}
test \lstinline|code| test
\end{document}

Related

How do I send the result of \pgfkeys to Lua?

In the example below the macro "\pgfkeysprintvalue" is a variant of "\pgfkeysgetvalue" that sort of sends the value of a key to Lua, and prints it from Lua:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\pgfkeyssetvalue{/p}{PP}
\pgfkeysgetvalue{/p}{\luatmp}
\luatmp % prints a "PP" in the document
\def\pgfkeysprintvalue#1{
\pgfkeysgetvalue{#1}{\luatmp}
\directlua{print("#1: "..(token.get_meaning("luatmp") or ""))}
}
\pgfkeyssetvalue {/q}{QQ}
\pgfkeysprintvalue{/q} % prints "/q: ->QQ" on stdout
\end{document}
In this other example the first three "\pgfkeys"s have interesting side-effects but expand to nothing, and the "\pgfkeys{/c,/d,/e=DD}" expands to "(c)(d:)(c)(d:DD)":
\documentclass{article}
\usepackage{tikz}
\begin{document}
\pgfkeys{/c/.code=(c)}
\pgfkeys{/d/.code=(d:#1)}
\pgfkeys{/e/.style={/c,/d=#1}}
\pgfkeys{/c,/d,/e=DD}
\end{document}
The last "\pgfkeys" above "prints" the "(c)(d:)(c)(d:DD)" into the document, and it appears in the PDF.
How do I write a variant of \pgfkeys - let me call it \pgfkeysprint - that would store the expansion of its argument in a macro called \luatmp? I guess that it would be like this:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\def\pgfkeysprint#1{
<I don't know what to put here>{#1}{\luatmp}
\directlua{print("#1: "..(token.get_meaning("luatmp") or ""))}
}
\pgfkeys{/c/.code=(c)}
\pgfkeys{/d/.code=(d:#1)}
\pgfkeys{/e/.style={/c,/d=#1}}
\pgfkeysprint{/c,/d,/e=DD} % prints "/c,/d,/e=DD: -> (c)(d:)(c)(d:DD)" on stdout
\end{document}
I'm trying to write functions for inspecting PGF keys from a Lua REPL running inside LuaLaTeX, and the part corresponing to the "<I don't know what to put here>" above is the main piece that is still missing...
Obs: I tried to write my macro \pgfkeysprint by adapting the definition of \pgfkeysgetvalue in pgf/utilities/pgfkeys.code.tex, but I couldn't find the right way to use the "\expandafter"s...

Latex: \newcommand interpret # symbol problem

I defined a new command
\newcommand{\test}[1]{\href{https://github.com/microsoft/vscode/blob/main/package.json#1}{#1}}
When I use it as follows:
\test{#L4}
the url will be interpreted as:
https://github.com/microsoft/vscode/blob/main/package.json##L4
There is an extra # in the url, which is unexpected. What I really want is:
https://github.com/microsoft/vscode/blob/main/package.json#L4
which means line 4 of the package.json code
The easiest but no so elegant way to solve the problem is as follows.
\test{\#L4}
but what if other special characters like _ appear in the part of url I copy? It's boring to correct these meaningless grammar mistakes.
Is there any more elegant way to solve the problem? What I want is to copy plain text, which is part of url to the latex code and no extra efforts like adding \ escape character before # and other special characters are needed.
What I want is to copy plain text, which is part of url to the latex code and no extra efforts like adding \ escape character before # and other special characters are needed.
I am afraid it will require extra effort because # is used as a parameter for macros in LaTeX. You can play with redefining categories and when # is consumed, restore its original meaning.
I have another solution based on expl3 where I save the main address and create links with additional parts. See a small demo below
\documentclass{article}
\usepackage[colorlinks]{hyperref}
\usepackage[margin=1in]{geometry} % To fit long links without breaks
\ExplSyntaxOn
\str_new:N \l__xaddr_main_str
\str_new:N \l__xaddr_show_str
\cs_new:Npn \combine_addr:n #1 {
\str_set:Nn \l_tmpa_str {#1}
\str_concat:NNN \l__xaddr_show_str \l__xaddr_main_str \l_tmpa_str
}
\NewDocumentCommand\xsetmainaddr{v}{
\str_set:Nn \l__xaddr_main_str {#1}}
\NewDocumentCommand\xdisplay{v}{%
\str_if_empty:NF \l__xaddr_main_str {
\combine_addr:n {#1}
\str_use:N \l__xaddr_show_str
}}
\NewDocumentCommand\xhref{vv}{%
\str_if_empty:NF \l__xaddr_main_str {
\combine_addr:n {#1}
\href{\l__xaddr_show_str}{#2}
}}
\ExplSyntaxOff
\setlength\parindent{0pt} % To fit long links without breaks
\begin{document}
\xhref{#L4}{GitHub} % Nothing to display. The main address is not defined.
\xsetmainaddr{https://github.com/microsoft/vscode/blob/main/package.json}
\par\xdisplay{#L4} % Display the full address including #L4
\par\xdisplay{#something_else%20here} % all characters accepted
\par\xhref{#L4}{GitHub} % Generates link: <address>#L4
\bigskip
\xsetmainaddr{http://www.google.com} % New address
\par\xhref{}{Google} % Generates link
\end{document}

Howto - Latex syntax with \section[]{}: Putting Brackets [ Inside of Brackets

I am writing a beamer presentation. My problem is a syntax issue with \section[Outline]{title}. \section is working fine with all of my section titles except in the following example:
\section[$\mathbb{Z}[G]$-module constructions]{$\mathbb{Z}[G]$-modules and Geometric Constructions}
I need a [ or \[ symbol around the G (i.e., [G]) in the Outline part of \section,however, Tex throws tons of errors whenever anything resembling a bracket is in there.
All ideas are welcome and thanks in advance.
Michael Anderson points out a much cleaner way of doing the same in the comments. Define the command in your preamble as
\newcommand[1]{\bracket}{[#1]}
and use the syntax $\bracket{G}$ to place brackets around G.
The problem is because LaTeX interprets the ] in [G] as a closing bracket for the [Outline] part of the command. Here's a work around for that. In your preamble, add the following:
\newcommand{\rbracket}{]}
Now define your section as
\section[$\mathbb{Z}[G\rbracket $-module constructions]{$\mathbb{Z}[G]$-modules and Geometric Constructions}
This is how the outline should look like
By wrapping the short title in an additional layer of {...} you can "hide" the problematic content from latex:
\documentclass{beamer}
\begin{document}
\begin{frame}
\tableofcontents
\end{frame}
\section[{$\mathbb{Z}[G]$-module constructions}]{$\mathbb{Z}[G]$-modules and Geometric Constructions}
\begin{frame}
content...
\end{frame}
\end{document}
Michael Anderson gave a nice solution, but when I tried it, I got errors. Then I wrote this:
\newcommand{\bracket}[1]{[#1]}
and that worked to get brackets in brackets with:
\bracket{content}
to get [content].

How to embed LaTeX keywords inside a LaTeX document using 'listings'

I want to cite LaTeX code into my document but how do I embed the keywords "\begin{lstlisting}" and "\end{lstlisting}" correctly?
CODE BELOW DOES NOT WORK (obviously):
\lstset{language=TeX, basicstyle=\footnotesize, numbers=left, numberstyle=\tiny, frame=single}
\begin{lstlisting}
\begin{lstlisting} % this is code
place your source code here % this is code
\end{lstlisting} % this is code
\end{lstlisting}
Do you have \usepackage{listings} in your preamble? If so, it should work. TeX is a supported language.
Here's a minimal example:
\documentclass{article}
\usepackage{listings}
\begin{document}
This is a StackOverflow test file.\\
To use \texttt{lstlisting}, include this in the preamble:
\begin{lstlisting}
\usepackage{listings}
\end{lstlisting}
Hope that helped :)
\end{document}
which compiles to
EDIT
To quote commands from the listings package (actually, only for \end{lstlisting}), escape to latex to print the \ character and you're all set. In the following, I've defined # as the escape character and everything within two # symbols is typeset in LaTeX. So here, I've input the \ using LaTeX and the rest within lstlisting and the \end{...} sequence is not interpreted as a closing the environment.
\documentclass{article}
\usepackage{listings}
\begin{document}
This is a StackOverflow test file.\\
Use escape characters to escape to \LaTeX
\lstset{escapechar=\#}
\begin{lstlisting}
\begin{lstlisting}
some code here
#\textbackslash#end{lstlisting}
\end{lstlisting}
Hope that helped :)
\end{document}
The output is
can you use a verbatim block?
\begin{verbatim}
\begin{lstlisting} % this is code
place your source code here % this is code
\end{lstlisting} % this is code
\end{verbatim}
You can use
\lstnewenvironment{OtherListing}
{}
{}
to create a new envirnment that behaves just list lstlisting, and \end{lstlisting} should not be forbidden in it.

How to create a white-on-black style for LaTeX listings

I've been looking at Philip Bunge's post on how to create a "Tango" style with LaTeX listings, and trying to adapt this to make the default text style white and the background black (this is for slides, not an article!). This is what I added:
\definecolor{Black}{gray}{0.0}
\definecolor{White}{gray}{0.9}
...
\lstset{
basicstyle=\color{White},
backgroundcolor=\color{Black},
...
}
This assumes that basicstyle sets the default style of all text. The listings documentation says this:
basicstyle is selected at the beginning of each listing. You could use \footnotesize,
\small, \itshape, \ttfamily, or something like that. The last token of must not read any following characters.
The output of this still shows "default" text as black. It is possible to set more style directives that cover most tokens in a given programming language, but even doing this some tokens (such as brackets and other punctuation) will be missed. What did I do wrong?
The following code worked for me, but I converted the .dvi file to a .pdf in order to have the text appear as white, so it might have been your viewer? I'm using xdvi and xpdf.
\documentclass[a4paper, 11pt]{article}
\usepackage{listings}
\usepackage{color}
\definecolor{theWhite}{gray}{0.9}
\definecolor{theBlack}{gray}{0.0}
\lstset { basicstyle=\color{theWhite}, backgroundcolor=\color{theBlack} }
\begin{document}
\begin{lstlisting}
((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))
\end{lstlisting}
\end{document}
I hope that helps!

Resources