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.
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this communityIs 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.
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:
\DeclarePairedDelimiter
macro works — you can also make it use a specific size if you want to: \floor[\Bigg]{\frac{x}{2}}
.
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
.
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.
\newcommand
not \def
. For 1. and 2. using \left...\right
is not appropriate in a number of situations.
May 5, 2018 at 8:40
\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.
This will also work fine without using mathtools.
\newcommand{\floor}[1]{\lfloor #1 \rfloor}
\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
\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
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
.
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.