252

Is there a macro in latex to write ceil(x) and floor(x) in short form? The long form

\left \lceil{x}\right \rceil 

is a bit lengthy to type every time it is used.

3
  • 3
    Welcome to TeX.SX! Your post was migrated here from another Stack Exchange site. Please register on this site, too, and make sure that both accounts are associated with each other (by using the same OpenID), otherwise you won't be able to comment on or accept answers or edit your question.
    – Joseph Wright
    Jun 8, 2013 at 5:31
  • Thank you, it seems I was asking questions in the wrong forum indeed!
    – danny
    Jul 7, 2013 at 17:35
  • See also: tex.stackexchange.com/q/42271/47927 Oct 25, 2021 at 22:56

4 Answers 4

273

Using \DeclarePairedDelimiter from mathtools, you could define macros \ceil and \floor, which will scale the delimiters properly (if starred):

\documentclass{minimal}
\usepackage{mathtools}
\DeclarePairedDelimiter\ceil{\lceil}{\rceil}
\DeclarePairedDelimiter\floor{\lfloor}{\rfloor}

\begin{document}
  \begin{equation*}
    \floor*{\frac{x}{2}} \leq \frac{x}{2} \leq \ceil*{\frac{x}{2}}
  \end{equation*}
\end{document}

Result:

example of using floor and ceiling

8
  • 4
    That's neat. Didn't know about this command.
    – Jan
    Jun 8, 2013 at 11:47
  • 3
    Just a question: why * when using \floor? I noticed that if the * is omitted from the command, the delimiter does not resize... but why?
    – Gherardo
    Jul 11, 2015 at 7:26
  • 4
    @Gherardo: Check the documentation of the mathtools package. It's just the way the \DeclarePairedDelimiter macro works — you can also make it use a specific size if you want to: \floor[\Bigg]{\frac{x}{2}}.
    – You
    Jul 11, 2015 at 10:10
  • 40
    As a minor side note, the inequality you used as an example is incorrect for all even numbers.
    – Chris
    Feb 18, 2018 at 13:35
  • 4
    @Atom The post was edited after I made this comment. tex.stackexchange.com/review/suggested-edits/232037
    – Chris
    Aug 23, 2022 at 16:29
41
  1. You can define your own macro via the \def command anywhere in your document. For example

    \def\lc{\left\lceil}   
    \def\rc{\right\rceil}
    

    and then just write \lc x \rc.

  2. Or you use the \providecommand in the preamble, e.g.

    \providecommand{\myceil}[1]{\left \lceil #1 \right \rceil }
    

    to simply use \myceil{x} in your document.

  3. Use an editor, like vim, that allows for defining shortcuts for quick and efficient editing.
  4. And, finally, don't forget about readability of your tex document. Check out this thread for some instructive comments on how to write efficient and readable tex math docs.
3
  • 4
    For 1. In LaTeX you should use \newcommand not \def. For 1. and 2. using \left...\right is not appropriate in a number of situations. May 5, 2018 at 8:40
  • How does \providecommand differ from \newcommand?
    – Paul Wintz
    Nov 15, 2021 at 10:00
  • 1
    @PaulWintz If the command already exists, \providecommand will do nothing and \newcommand will cause an error. If the command doesn't exist, they are equivalent. There's also \renewcommand, which will cause an error if the command doesn't already exist. The purpose of the three commands is to make sure you realize when you're overwriting an existing command.
    – Teepeemm
    Jun 8, 2022 at 20:56
22

This will also work fine without using mathtools.

\newcommand{\floor}[1]{\lfloor #1 \rfloor}
2
  • 6
    Welcome to TeX.SX! \floor and \rfloor are amsmath commands, mathtools builds on top of amsmath, so it's no wonder, this would work even without mathtools. The solution with \DeclarePairedDelimiter shows better spacing however. Perhaps you should elaborate on your answer and show some screenshot and a full example, not only fragments of code
    – user31729
    Jun 24, 2015 at 23:47
  • 2
    @ChristianHupfer \lfloor and \rfloor are in core LaTeX. \floor is not defined in amsmath. The \DeclaredPairedDelimiter' is good, but in comparison to the \newcommand` above it mostly provides an easy way to change the code when a different size is required. May 5, 2018 at 8:44
20

There is no need to use mathtool here:

\newcommand{\floor}[1]{\left\lfloor #1 \right\rfloor}
\newcommand{\ceil}[1]{\left\lceil #1 \right\rceil}

is better than mathtool.

4
  • 3
    I think this is a comment to one of the previous answers, not an answer...
    – CarLaTeX
    May 5, 2018 at 8:06
  • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
    – Bobyandbob
    May 5, 2018 at 8:21
  • 2
    @Bobyandbob This most certainly answers the question. It might not be a very good answer (that is certainly my belief), but there should be little doubt that this is an answer to the question asked above.
    – moewe
    May 5, 2018 at 11:48
  • 1
    I strongly disagree with the sentiment that the approach here is better than mathtool. The mathtool approach allows for automatic scaling (which should be used with care, if not avoided at all, because it can give sub-par results in certain cases, see e.g. here - could not find a better example) and manual specification of fence size.
    – moewe
    May 5, 2018 at 11:52

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