10

I'm trying to typeset some computer code which involves a caret (for powers); for example

\verb!x^3+x^2-1!

The trouble here is that the caret produced is rather flat. I could use a wedge instead:

\newcommand{\pwr}{${}^\wedge}$}
\texttt{x\pwr 3+x\pwr 2-1}

but the wedge here is too pointy, as well as being the wrong weight for the typewriter font. What I want to know is: is there a symbol, which comes halfway between caret and wedge in shape, and is the same weight as a typewriter caret (so can be used within \texttt{})?

I've tried replacing \verb!^! with {{}\large\textasciicircum} and this sort of works, but is still clearly not an optimum solution.

8

The appearance of the care symbol depends on the used font. For example, Courier or TeX Gyre Cursor has a symbol that is something between the caret of the Computer Modern fonts and the \wedge solution.

The following example uses \lstinline instead of \verb, because a symbol can more easily be replaced via option literate:

\documentclass{article}
\usepackage{listings}
\newcommand*{\caret}{%
  \begingroup
    \fontencoding{T1}%
    \fontfamily{qcr}% TeX Gyre Cursor
    %\fontfamily{pcr}% Courier
    \selectfont
    \string^%
  \endgroup
}
\lstdefinestyle{caret}{
  basicstyle=\ttfamily,
  literate={^}{\caret}{1},
}
\newcommand*{\lstverb}{\lstinline[style=caret]}

\begin{document}
\begin{tabular}{ll}
\verb|\verb|: &
  \verb|x^3+x^2-1| \\
\verb|\wedge|: &
  \texttt{x$^\wedge$3+x$^\wedge$2-1} \\
\verb|\lstinline| + \verb|\caret|: &
  \lstverb|x^3+x^2-1| \\
\end{tabular}
\end{document}

Result

5

An admittedly complicated attempt for automatic use of a different symbol in verbatim modes; I use a vertically scaled circumflex in a "bold non extended font".

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\newcommand\scalecircum{%
  \leavevmode\smash{%
    \ooalign{%
      \hidewidth
      \raisebox{-.75ex}{%
        \scalebox{1}[1.5]{\normalfont\fontseries{b}\selectfont\char`\^}%
      }%
      \hidewidth\cr
      \hphantom{n}\cr
    }%
  }%
}
\makeatletter
\def\activate@scalecircum{%
  \begingroup\lccode`\~=`\^
  \lowercase{\endgroup\def~}{\scalecircum}%
  \catcode`\^=\active}
\expandafter\def\expandafter\@noligs\expandafter{\@noligs\activate@scalecircum}
\makeatother

\begin{document}
\verb|x^2+y^2| --- \texttt{x\textasciicircum2+y\textasciicircum2}

\verb|x_1+y_1|
\end{document}

enter image description here

You can compare the result with the usual one (first line right) and see that the symbol occupies the correct amount of space.

2

You can use the scalerel package to stretch the wedge symbol to the desired height. In the example below, I first show your two original attempts, followed by the wedge at 60% of its original height. Obviously, any fractional setting can be used.

\documentclass{article}
\usepackage{scalerel}
\begin{document}

\verb!x^3+x^2-1!

\newcommand{\pwr}{${{}^\wedge}$}
\texttt{x\pwr 3+x\pwr 2-1}

\renewcommand{\pwr}{${{}^{\vstretch{.6}{\wedge}}}$}
\texttt{x\pwr 3+x\pwr 2-1}

\end{document}

enter image description here

1

Thanks, folks. I ended up making my own character with a tikz kludge:

\usepackage{tikz}
\newcommand{\crt}{\tikz[baseline=0ex]{%
    \draw[color=white] (0,0) rectangle (1.3ex,1.5ex);%
    \draw[line width=1pt,line cap=round] (0.15ex,1.2ex) -- (0.65ex,1.7ex) -- (1.15ex,1.2ex);}}

and then in the body of the document:

\texttt{x\crt 3+x\crt 2-1}

Clearly my kludge is not very portable; I would need to be smarter in having TiKZ use the dimensions of the current characters, so it would always be the correct size.

enter image description here

  • What do you need the white rectangle for? – einpoklum Jun 20 '13 at 20:12
  • For a bounding box: otherwise the spacing isn't right. I could replace this with \draw[white,use as bounding box] (0ex,0ex) rectangle (1.4ex,2ex); As it is the spacing is just right for cmtt10. – Alasdair Jun 20 '13 at 21:07
  • But what if the background is non-white, or not even a uniform color? Can't you draw a 'phantom' or transparent rectangle? – einpoklum Jun 21 '13 at 13:18
  • Sure, in fact what I've done since is to use \draw[opacity=0...] which provides for complete transparency. And I've found a way to use the size of the character: \newcommand*{\crt}{\tikz[baseline=(X.south),inner sep=0pt]{\node[opacity=0] (X) {\char88};\draw[line width=0.7pt,line cap=round,scale=.75] (X.north west) -- ($(X.north west)!0.5/cos(40)!40:(X.north east)$) -- (X.north east);}} I've found that an angle of 40 for the caret seems to given (at least to me) the most pleasing result. – Alasdair Jun 21 '13 at 23:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.