85

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
2

4 Answers 4

66

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.

3
  • 2
    Ah, another reason to use \usepackage[T1]{fontenc}. I've added this to another answer, but could you explain why this happens, exactly? (Either here or at the post on texblog.) Aug 15, 2010 at 19:06
  • @ShreevatsaR: I've added links to the encoding tables and a brief explanation.
    – Stefan Kottwitz
    Aug 15, 2010 at 19:46
  • There's also a encoding table at latex-project.org/help/documentation/encguide.pdf
    – user202729
    Jun 26, 2022 at 7:55
43

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.

4
  • 6
    \usepackage[T1]{fontenc} should be added anyway, because it fixes a lot of font-related problems.
    – Caramdir
    Jan 19, 2011 at 16:50
  • 2
    I wasn't aware of $\vert$. I've been using $\mid$, which looks identical to me.
    – DJP
    Jul 29, 2011 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. Sep 15, 2015 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 5, 2021 at 9: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).

4
  • 2
    I am not sure if you know the difference between input encoding and font encoding. Maybe the following question is interesting for you: tex.stackexchange.com/questions/664/…
    – matth
    Jan 18, 2012 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, 2012 at 15:06
  • \texttt{A\textbar{}B} produces homogeneous formatting.
    – Erwann
    Jan 22, 2022 at 5:54
  • The {} is redundant and kind of bad practice as well (it breaks kerning). Just do \texttt{A\textbar B}.
    – user202729
    Nov 18, 2022 at 4: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.

2
  • I've recently been told that this is also supported in TeX Live with -shell-escape. Aug 21, 2010 at 2:00
  • 5
    Not that this has anything to do with the original question :) Aug 21, 2010 at 2:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .