LaTeX, tildes and verbatim mode - latex

Following on from this question, I'm totally stumped on getting LaTeX to give me a tilde when I'm in verbatim mode. It has to be a tilde because it's the type of a function!
sig symm : (Board, [(Int, Int)]) ~> Bool
Standard methods for displaying a tilde are printed verbatim, of course..
Any suggestions?
An edit to clarify: Typing a ~ in verbatim mode gives an accent above a blank space. I'm after a tilde as it appears at the beginning of this sentence.

If there are some characters that do not occur in your input, you can use fancyvrb and its commandchars option to insert TeX commands within verbatim text:
\documentclass{article}
\usepackage{fancyvrb}
\begin{document}
\newcommand{\mytilde}{$\sim$}
\begin{Verbatim}[commandchars=\\\{\}]
sig symm : (Board, [(Int, Int)]) \mytilde> Bool
\end{Verbatim}
\end{document}
See the documentation of fancyvrb for more.

Perhaps you should look at: LaTeX package to do syntax highlighting of code in various languages which has suggestions for typesetting code...
I assumed that listing would do it for you, but failing that alltt and fancyvrb are alternatives to verbatim. See this search on CTAN for other possibilities.

If you are using listing command, you can set the tilde to be literal. Likt this.
\documentclass
\usepackage{listings}
\lstset{
literate={~} {$\sim$}{1} % set tilde as a literal (no process)
}
\begin{document}
\begin{lstlisting}
~
\end{lstlisting}
\end{document}

\begin{verbatim}
~
\end{verbatim}

Related

Code in latex for braces

In LaTeX text, I sometimes need 'unpaired' parenthesis - ( without ) or vice versa; however, in such a case, delimiter check macro reports error. I found a simple solution: \symbol{40} and \symbol{41}, which aren't recognized as delimiters. The same works for brackets [ and ] (decimal codes are 91 and 93 respectively), but doesn't work for braces { and }. Their codes are 123 and 125 respectively, but \symbol{123} and \symbol{125} produce something like dash and quote. I use fontenc LCY, trying both text and math modes.
I've found a solution:\textbraceleft and \textbraceright, but why \symbol doesn't work in such a case?
Found it! This first code
\documentclass[border=5mm]{standalone}
\begin{document}
\symbol{123} and \symbol{125}
\end{document}
gives
as output, while
\documentclass[border=5mm]{standalone}
\usepackage[T1]{fontenc}
\begin{document}
\symbol{123} and \symbol{125}
\end{document}
gives this output instead:
Adding
\usepackage[T1]{fontenc}
provides the desired font encoding (fontenc package).
A (not-so-further) reading suggestion: font encoding vs. input encoding.

non-ASCII characters in Julia sourcefiles (e.g. θ) come out mojibake (e.g. Îÿ) using '\lstinputlisting{}' in LaTeX

I would like to include some Julia source in a LaTeX doc. I am familiar with the 'listings' package, and have crafted a decent 'lstdefinelanguage'. But I am having trouble with non-ASCII characters in the jl. I have played around with various 'lstset' and 'inputenc' options like 'extendedchars' and 'utf8' to no avail. I am using TeXShop on a Mac. Has anyone cracked this?
You can use literate option although it requires some set-up work. Here is a MWE:
\documentclass{article}
\usepackage{listings}
\usepackage{courier}
\lstset{basicstyle=\ttfamily}
\lstset{literate=
{δ}{{$\delta$}}1
{ϵ}{{$\epsilon$}}1
}
\begin{document}
\begin{lstlisting}
function f()
ϵϵϵϵϵ = 1
xxxxx = 3
δδδδδ = 2
ϵ + x + δ
end
\end{lstlisting}
\end{document}

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.

Unicode characters in XeLatex

This is my LaTex script:
\documentclass[a4paper]{article}
\usepackage{fontspec}
\usepackage{xunicode}
\usepackage{xltxtra}
\setmainfont[Mapping=tex-text]{Jenson Classico}
\begin{document}
ąęśćłó
\end{document}
I compile this using xelatex through eclipse plugin.
As Jenson Classico has no polish characters I have edited this font to add them. In Microsoft Office it works fine (I am able to use polish characters). Unfortunately in pdf generated from above tex-file it doesn't. If I use any other font (e.g. Arial) it works like a dream.
Any suggesions? I really don't have any more ideas...
Try using the polyglossia package.
\documentclass[a4paper]{article}
\usepackage{fontspec}
\usepackage{polyglossia}
\usepackage{xunicode}
\usepackage{xltxtra}
\setdefaultlanguage{polish}
\setmainfont[Mapping=tex-text]{Jenson Classico}
\begin{document}
ąęśćłó
\end{document}
Use \char" followed by the Unicode value.
For example, ąęśćłó would be: \char"0105\char"0119\char"015B\char"0107\char"0142\char"00F3.

how write this in verbatim latex

i have a little question about the verbatim environment in latex.....
my problem is how can i write "\end{verbatim}" inside of a verbatim environment, it supposed that everything inside a verbatim is written just like it looks but this is the string to end the environment, so how can i do this?
\\end{verbatim}
Should do it by making the initial \ into a literal backslash.
I think it might be impossible in a verbatim environment -- at least, that's what Lamport says in the LaTeX book (first paragraph on pg. 64). However, you can do it with the \verb command: \verb+\end{verbatim}+ works just fine (and in fact, you can use any character instead of + as long as you make them match).
That said, the listings package is smart enough to have an escape character. For example,
\begin{lstlisting}[escapechar=+]
+\verb!\end{lstlisting}!+
\end{lstlisting}
does exactly what you're looking for. (Again, pick a different character than + if you're already using that for something else.)
Use the fancyvrb package instead of the default verbatim package.
Craig's answer is good. Alternatively, \expandafter\string\csname end{verbatim}\endcsname.
These solutions won't linebreak nicely: fixing this is fiddly.
I have a simpler solution. Write a non-breaking space, in Emacs it's C-x 8 SPC (Windows, though I haven't tried it is Alt+0160 (on numeric keypad)). So, you can do this:
\begin{verbatim}
\_end{verbatim}
\end{verbatim}
where the first \end{verbatim} has this "_", the non-breaking character.
Here is my method, thanks #S.Lott for his advice.
If you want to write \end{verbatim} in the verbatim environment, you have to using another 'verbatim' layer to nest your code (this is where fancyvr package comes to play). Here it is:
\documentclass{article}
\usepackage{fancyvrb}
\begin{document}
Some text here
\begin{Verbatim} % note 'uppercase V' in verbatim
\begin{verbatim}
Code example
\end{verbatim}
\end{Verbatim}
\end{document}

Resources