Hot answers tagged typewriter
32
The short answer is you use \verb where you need to write a small piece of inline verbatim material that contains characters TeX treats (or rather, is currently treating) as special. \texttt is for when you just want typewriter font.
\verb has some downsides, such as not working in moving arguments. In those cases, you're probably better off using \texttt ...
26
In LaTeX it is standard behavior that typewriter fonts do not do any
hyphenation because it is typically used for code. Thus, the fonts
used normally for \texttt all suppress hyphenation automatically.
To change this, there are essentially three options:
enable hyphenation for the fonts used by \texttt throughout the document
define your own variant of ...
18
Informally speaking, TeX break lines at spaces (and a few other positions in a word, called "discretionary break"). Discretionary break is not allowed in typewriter typesetting. If there is no space in \texttt{}, it cannot break.
For your example, there is no help using \texttt instead of \verb. There are several ways to solve such kind of problem:
Enable ...
16
Hyphenation and full justification is possible with typewriter text as well. Here's a command \justify for this purpose, shown with the example above:
\documentclass{minimal}
\usepackage{lipsum}
\newcommand*\justify{%
\fontdimen2\font=0.4em% interword space
\fontdimen3\font=0.2em% interword stretch
\fontdimen4\font=0.1em% interword shrink
...
16
The default was chosen by the package author, according to the common way of setting URLs. Using a monospaced font helps distinguishing them, and this is the main reason.
However the font can be changed with \urlstyle that accepts one argument among
tt
rm
sf
same
The default is equivalent to \urlstyle{tt}; with \urlstyle{rm} and \urlstyle{sf} the font ...
14
basicstyle=\footnotesize\ttfamily,
or with package microtype
\usepackage[T1]{fontenc}
\usepackage[scaled]{beramono}
\newcommand\Small{\fontsize{9}{9.2}\selectfont}
\newcommand*\LSTfont{\Small\ttfamily\SetTracking{encoding=*}{-60}\lsstyle}
...
\begin{lstlisting}[basicstyle=\LSTfont,...]
...
Which gives a better result.
14
Use the upquote package; even if the package documentation doesn't mention alltt, it works also with it:
\documentclass{article}
\usepackage{alltt}
\usepackage{upquote}
\usepackage{color}
\usepackage{fullpage}
\begin{document}
\begin{center}
\LARGE hello.py
\end{center}
\begin{alltt}
{\color{red}print} 'hello world'
\end{alltt}
\end{document}
Notice ...
13
My humble attempt with the xstring package:
\documentclass{article}
\usepackage{tikz}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{0pt}
\usepackage{xstring}
\def\mytext{Hello world}
\StrLen{\mytext}[\mylen]
\begin{document}
\foreach \t in {0,...,\mylen}{%
\begin{tikzpicture}[scale=1]
...
13
\verb and verbatim assume the font has these characters in their ascii positions and locally makes the characters be catcode 12 (like punctuation) with no special defintion.
\textbackslash (and friends) is defined to be a encoding-specific command and (to fake an air of sanity over the original TeX encodings) LaTeX assumes that OT1 encoding is the encoding ...
12
I suggest Latin Modern Mono Light family. In Plain TeX:
\font\tt=rm-lmtl10
\font\itt=rm-lmtlo10
\font\btt=rm-lmtk10
\font\bitt=rm-lmtko10
\tt Hello\par
\itt Hello\par
\btt Hello\par
\bitt Hello\par
\bye
In LaTeX, it is lmtt family in OT1 font encoding. See ot1lmtt.fd for more information.
Latin Modern fonts are available in Type1 and OpenType. ...
12
As Martin mentioned in the comment you need a font which provides such a combination. In the following example you can see that the font courier has this combination implemented instead of Computer Modern.
\documentclass{article}
\usepackage{listings}
\begin{document}
% Default Computer Modern font (no bold implemented)
\renewcommand{\ttdefault}{cmtt}
...
12
The easiest (but not the most flexible) way is to use memoir with the ms option. Then you get something that look like a good, old typewriter manuscript.
memoir also hard code the monospace font to be cmtt, se line 12643 in the memoir.cls. If you want another monospaced font, you need to change at least the lines:
\renewcommand{\familydefault}{cmtt}
...
12
The Open Type Version of Libertine has also a typewriter version. However, for pdflatex you can use the Bera Mono:
\documentclass{scrartcl}
\usepackage[T1]{fontenc}
\usepackage{libertine}
\usepackage[scaled=0.83]{beramono}
\begin{document}
This is a test with \texttt{some \textbf{bold} typewriter text}.
\end{document}
If you want Bera Mono for LuaTeX ...
11
As Will Robertson says, the rendering of the caret depends on the font. Here I give three examples:
Computer Modern Typewriter in OT1 encoding
Computer Modern Typewriter in T1 encoding
Inconsolata
In all three the first caret is obtained with \textasciicircum and the second one with \^{}. In the first row the two carets are the same, in the second row ...
11
You could redefine \texttt to take an optional argument that allows you to switch colour if necessary, and if not, stick to some default colour.
In the minimal example below, \texttt[<color>]{<stuff>} has been redefined to take an optional <color> (default is black).
\documentclass{article}
\usepackage{xcolor}% ...
11
By design, the + in Computer Modern has its horizontal bar slightly above what we would consider the visual center; this "fools" TiKZ's algorithm for centering an object on the node coordinates.
\documentclass[border=1]{standalone}
\newcommand{\ttplus}{%
\raisebox{-.15ex}[\dimexpr\height-.15ex\relax][0pt]{\ttfamily+}}
\usepackage{tikz}
\begin{document}
...
11
Just to add to egreg's fine answer. Why it moves up? TikZ will center the enclosing box, as is obvious from the image below:
TeX aligns the letters symbols at their baseline in text mode and at a centerline, if they are in maths mode. Kerning is a partial solution as egreg, showed. Having the + in math is a better solution. It is not just a font issue, ...
11
It seems that the "Lithuanian package" invoked by the L7x option doesn't set correctly the default typewriter type family: the following way to set up the document should work.
\usepackage[L7x]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[lithuanian]{babel}
\renewcommand{\ttdefault}{lmtt}
11
Monospaced fonts in the "old TeX world" usually don't allow for stretching and shrinking spaces. Here's the beginning of cmtt10.pl, which is the human readable version of the metric file cmtt10.tfm:
(FAMILY CMTT)
(FACE O 352)
(CODINGSCHEME TEX TYPEWRITER TEXT)
(DESIGNSIZE R 10.0)
(COMMENT DESIGNSIZE IS IN POINTS)
(COMMENT OTHER SIZES ARE MULTIPLES OF ...
11
Inconsolata fits well with Linux Libertine, and has a bold face:
\documentclass{scrartcl}
\usepackage{fontspec}
\usepackage{xunicode}
\defaultfontfeatures{Ligatures=TeX}
\setmainfont{Linux Libertine O}
\setmonofont{Inconsolata}
\begin{document}
This is a test with \texttt{some \textbf{bold} typewriter text}.
\end{document}
11
You can use \verb|operator^=| to print text verbatim.
Or with the listings package you can use \lstinline|^=| to set verbatim text.
If you do this a lot, you can use the command \lstMakeShortInline| to allow you to just type |^=| to set these things. You need to pick a character that won't appear inside your verbatim text. That is, you couldn't do ...
10
You can use \verb to display a backslash in monospaced font; the following example shows another option to display a backslash in monospaced font:
\documentclass{article}
\begin{document}
\verb+\+
\texttt{\symbol{92}}
\verb+\integrate+
\texttt{\symbol{92}integrate}
\end{document}
10
I like Inconsolata like Khaled does. It's monospaced and it supports several encodings including T1, OT1 and LY1.
Just load inconsolata.sty, you could additionally specify a scaling option [scaled=factor].
Here's an example how the font looks like, taken from my blog:
Links:
Inconsolata Homepage
Inconsolata on CTAN
documentation by Karl Berry ...
10
You are missing a \ttfamily in the basicstyle. If this doesn't give you the size you want try \scriptsize or even \tiny instead of \footnotesize. You don't need to add the same size for the numberstyle again because basicstyle is used for everything by default.
\documentclass{article}
\usepackage{listings}
\lstset{language=C,
numberstyle=\footnotesize,
...
10
You can redefine the \texttt and \verb commands:
\documentclass{article}
\usepackage{xcolor}
\definecolor{ttcolor}{RGB}{255,110,120}
% redefinition of \texttt
\let\Oldtexttt\texttt
\renewcommand\texttt[1]{{\ttfamily\color{ttcolor}#1}}
% redefinition of \verb
\makeatletter
\def\verb{\relax\ifmmode\hbox\else\leavevmode\null\fi
\bgroup\color{ttcolor}
...
10
If it doesn't occur very often or you can use search and replace you can add a negative space by yourself to compensate for it:
\documentclass{article}
\begin{document}
Normal margin here. Normal margin here. Normal margin here.
Normal margin here. Normal margin here. Normal margin here. Normal margin here.
Abcdefgh abcdefgh abcdefgh abcdefgh abcdefgh ...
10
You can add
\everymath{\mathtt{\xdef\tmp{\fam\the\fam\relax}\aftergroup\tmp}}
to the document preamble (or the part of the document where you need this)
and
\everydisplay{\mathtt{\xdef\tmp{\fam\the\fam\relax}\aftergroup\tmp}}
if you need display math as well.
In then in addition you may need to force the initialisation by putting
\setbox0\hbox{$ ...
9
Sometimes using \verb|...| is better. For example if you copy paste a piece of code like __start: in a \texttt{} environment you might get an error as symbol "_" is not inside a math environment. And then you have to rewrite the code like this: "\texttt{\_\_start}". But why would you do this when you can just use:"\verb|__start|".
9
You may prefer the character from the tt font:
\documentclass{article}
\begin{document}
\texttt{Samp\_Dist\_Corr}
\verb|Samp_Dist_Corr|
\texttt{Samp\char`_Dist\char`_Corr}
Or probably better add \usepackage[T1]{fontenc} then all the above forms will use the character from the font.
\end{document}
9
Unfortunately the sig-alternate class uses a peculiar1 way to set the document fonts.
In particular, the \texttt macro selects the font family aett (which belongs to a definitely obsolete package) at size 9pt, independent of context. So you have two problems.
Use a boldface version of the Typewriter font.
Increase the font size in section titles.
...
Only top voted, non community-wiki answers of a minimum length are eligible
