A note:
\textasciitilde
and \textasciicircum
are unnecessarily verbose. Use \^{}
and \~{}
instead.
Some clarification is needed.
\^
and \~
themselves are not individual "characters" like \%
\_
or \#
are, but rather they're command to
"place an accent on the following character", like how \'a
gives á
.
So there must be "something" ({}
) that follows the \^
-- in particular {\^}
will not work. You can think of this as putting ^
on an empty base.
Nevertheless, in all cases, \^{}
and \~{}
gives the exact same result as typesetting the character at slot 94 and 126 from the font file directly.
You can test it yourself by compiling this document:
%! TEX program = pdflatex
\documentclass{article}
%\usepackage[T1]{fontenc} % optional
\begin{document}
\pagenumbering{gobble}
\texttt{\textasciicircum\^{}\symbol{94}\^a\^b}
\textasciicircum\^{}\symbol{94}\^a\^b
\texttt{\textasciitilde\~{}\symbol{126}\~a\~b}
\textasciitilde\~{}\symbol{126}\~a\~b
%{\^} % does not work!
\end{document}
Output:
This is because the command is implemented (after some composite-character table lookup) with TeX's primitive \accent
command, but:
so \accent ⟨accent⟩ ⟨nothing follows⟩
gives just \char ⟨accent⟩
.
Another note:
In OT1 font encoding, \texttt{...}
used with _{}\
does not give the best result.
Refer to:
\documentclass{article}
\usepackage[OT1]{fontenc}
\begin{document}
\pagenumbering{gobble}
\texttt{\& \% \$ \# \_ \{ \} \textbackslash}
\texttt{\& \% \$ \# \symbol{95} \symbol{123} \symbol{125} \symbol{92}}
\end{document}
Output:
There are a few ways to fix it:
Refer to my answer in https://tex.stackexchange.com/a/643285/250119 for an explanation what's going on.
<>
appear as¡¿
in OT1 encoding ■|
appear as—
in OT1 encoding ■ alternative way to write^
and~
■ how to make_|<>
fit in\texttt{}
"code font" (use T1 encoding or some other trick, see my answer there for explanation)