88

How to insert pipe symbol | in TeX (LaTeX)?

I have tried this sample with pdflatex tmp.tex

\documentclass{extarticle}
\begin{document}
\textpipe
\end{document}

And it give undefined control sequence error

This is pdfTeX, Version 3.1415926-1.40.10 (TeX Live 2009/Debian)
restricted \write18 enabled.
entering extended mode
(./tmp.tex
LaTeX2e <2009/09/24>
Babel <v3.8l> and hyphenation patterns for english, usenglishmax, dumylang, noh
yphenation, loaded.
(/usr/share/texmf-texlive/tex/latex/extsizes/extarticle.cls
Document Class: extarticle 1996/10/08 v1.0 Non Standard LaTeX document class
(/usr/share/texmf-texlive/tex/latex/base/size10.clo)
(/usr/share/texmf-texlive/tex/latex/base/exscale.sty))
No file tmp.aux.
! Undefined control sequence.
l.3 \textpipe
lockstep
  • 250,273
avsej
  • 1,205

4 Answers4

68

The pipe symbol may be used directly in the input and will produce the desired output if you use T1 font encoding. For example:

\documentclass{article}
\usepackage[T1]{fontenc}
\begin{document}
Pipe sign: |
\end{document}

Output:

enter image description here

Compare these tables to see the difference between the two font encodings: OT1 (Standard encoding) vs. T1 (Cork encoding). The OT1 table doesn't contain the pipe symbol, but it can be found in the T1 table. It's position is 7C. With OT1 7C means a wide dash, that's why we get a wide dash instead of the pipe symbol in standard encoding.

See Special LaTeX characters for some more information.

For using the command \textpipe loading a package like tipa or t4phonet is necessary.

Stefan Kottwitz
  • 231,401
45

It is unnecessary to load any additional packages unless there are other reasons to do so. Mathmode provides alternative symbols $\vert$ for a vertical pipe and $\Vert$ for a double pipe. These appear to be independent of encoding. Actually as a math symbol $|$ appears also to be encoding independent.

Andrey Vihrov
  • 22,325
ChrisDR
  • 551
  • 6
    \usepackage[T1]{fontenc} should be added anyway, because it fixes a lot of font-related problems. – Caramdir Jan 19 '11 at 16:50
  • 2
    I wasn't aware of $\vert$. I've been using $\mid$, which looks identical to me. – DJP Jul 29 '11 at 17:25
  • When I used this method my word exceeds the page width. How can I avoid that? My two words are \texttt{--}genotyping_mode GENOTYPE_GIVEN_ALLELES. – tommy.carstensen Sep 15 '15 at 10:27
  • 2
    The difference between $\mid$ and $\vert$ appears to be spacing around the symbol, which $\mid$ has and $\vert$ doesn't. Also, $|$ lacks spacing around it. Unfortunately, using $\bigg$ on $\mid$ results in an error. – shs Jan 05 '21 at 09:49
23

I would recommend using the command \textbar, since this does not require you to use different encoding or additional packages. For me this was a much better solution, as I was already committed to usepackage[utf8]{inputenc}. Also, I am using a system that puts UTF-8 data into LaTeX templates (escaping special characters), and this system cannot rely on any additional packages (other than UTF-8 encoding).

david
  • 353
  • 2
    I am not sure if you know the difference between input encoding and font encoding. Maybe the following question is interesting for you: http://tex.stackexchange.com/questions/664/why-should-i-use-usepackaget1fontenc – matth Jan 18 '12 at 13:05
  • 1
    Yes @matth, you are of course right. I guess when reading this question my eyes glazed over {fontenc} and I read in incorrectly. – david May 16 '12 at 15:06
  • \texttt{A\textbar{}B} produces homogeneous formatting. – Erwann Jan 22 '22 at 05:54
  • The {} is redundant and kind of bad practice as well (it breaks kerning). Just do \texttt{A\textbar B}. – user202729 Nov 18 '22 at 04:54
6

As a side note: MikTeX supports using pipes in \input if you supply the flag -enable-pipes. This is useful for calling external programs directly from your document.

Here is an example of getting input from Octave and a listing of the document files:

%& -enable-pipes
\documentclass{article}
\usepackage{amsmath}
\newcommand\octave[2][]{%
   \input"|octave -qf --eval 'format #1;disp(#2)'"
}

\begin{document}

Foo $\pi \approx \octave{pi}$ bar.
\begin{align}
   \pi           &\approx \octave[long]{pi} \\
   \sqrt 2       &\approx \octave[short]{sqrt(2)} \\
   \pi\exp(3\pi) &\approx \octave[short g]{pi*exp(3*pi)}
\end{align}

{\obeylines
List of files:
\input"|ls -l \jobname.*"
}

\end{document} 

I am using MikTeX on a Linux system. If you are running windows you must make changes to the example accordingly.

Martin Heller
  • 11,391