212

Is there a convenient way to typeset the floor or ceiling of a number, without needing to separately code the left and right parts? For example, is there some way to do $\ceil{x}$ instead of $\lceil x \rceil$?

3
  • 14
    In case anyone else got here looking for mathjax notation, in mathjax, \lceil and \rceil are the appropriate markups. Commented Aug 21, 2017 at 8:15
  • 12
    For me, $\lceil x \rceil$ was already the solution I was looking for ;) Commented Aug 29, 2019 at 16:24
  • 1
    See also: tex.stackexchange.com/q/118173/47927 Commented Oct 25, 2021 at 22:57

3 Answers 3

262
\usepackage{mathtools}
\DeclarePairedDelimiter{\ceil}{\lceil}{\rceil}

The command \ceil will do; if called as \ceil*{x} it will add \left and \right; you can also call it as

\ceil[\big]{x} \ceil[\Big]{x} \ceil[\bigg]{x} \ceil[\Bigg]{x}

to state explicitly the size of the delimiters.

6
  • 35
    Even after more than 10 years of LaTeXing one still learns some new tricks!
    – yo'
    Commented Jan 25, 2012 at 17:32
  • 26
    Don't tell me! I learn new tricks even after 25 years!
    – egreg
    Commented Jan 25, 2012 at 17:34
  • 1
    @jamaicanworm There is, I believe also on this site. But I won't tell you: use \left and \right only when they are really needed.
    – egreg
    Commented Apr 21, 2012 at 17:50
  • 2
    @jamaicanworm Swap definition of starred and non-starred command discusses switching the starred with non-starred version. Commented Nov 11, 2012 at 1:36
  • 12
    To typeset the floor function, just replace "ceil" with "floor". This may be obvious, but it might save you the trouble of consulting documentation. Commented Sep 30, 2013 at 7:19
24

Here is a simple xparse implementation of \ceil, similar to that provided by mathtools' \DeclarePairedDelimiter:

xparse implementation of \ceil

\documentclass{article}
\usepackage{xparse}% http://ctan.org/pkg/xparse
\NewDocumentCommand{\ceil}{s O{} m}{%
  \IfBooleanTF{#1} % starred
    {\left\lceil#3\right\rceil} % \ceil*[..]{..}
    {#2\lceil#3#2\rceil} % \ceil[..]{..}
}
\begin{document}
\[\ceil[\big]{x} \quad \ceil[\Big]{x} \quad \ceil[\bigg]{x} \quad \ceil[\Bigg]{x} \quad \ceil*[\big]{\frac{1}{2}}\]
\end{document}

The optional argument is ignored in the starred version of \ceil*[..]{..}.

1
  • 4
    You could eliminate the \IfNoValueTF {\lceil#3\rceil} if you used {s O{} m}. Then the second arg #2 will be defined so only need #2\lceil#3#2\rceil. Commented Jan 25, 2012 at 18:29
3

You can define your command with a simple single line as follow:

\newcommand{\ceil}[1]{\lceil {#1} \rceil}

The above command definition tells that your command takes one input [1] and uses that input between the predefined commands \lceil and \rceil via {#1}

Hope it helps.

2
  • Welcome to TeX.SE!
    – Mensch
    Commented Oct 14, 2021 at 19:30
  • @Mensch Thanks :)
    – eferay
    Commented Oct 14, 2021 at 19:39

You must log in to answer this question.

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