# Define a command differently in inline vs display mode

I would like to have a command \catlim which behaves differently in inline vs in display mode. Namely, in inline mode, it should be equivalent to \lim and in display mode, it should be equivalent to \varprojlim (or some nicer version of it) with arrow below the limit. Here is an example what the result should look like:

% arara: lualatex
\documentclass{article}
\usepackage{amsmath}
\usepackage{unicode-math}

\begin{document}

Inline $$\lim_{i \in \mathbb{N}} \mathbb{Z} / p^i \mathbb{Z}$$ and display:
\begin{equation*}
\varprojlim_{i \in \mathbb{N}} \mathbb{Z} / p^i \mathbb{Z}.
\end{equation*}

\end{document}

• Just don't forget that inline math style is also used in displayed math, e.g. fractions and the standard cases env. Oct 18, 2019 at 8:18
• Another alternative to the solutions below is the plain TeX command \mathchoice. Oct 19, 2019 at 0:32
• @Davislor: You are right, of course, but, as I say in my answer, we cannot tell exactly what the OP’s idea of “being in display mode” is.
– GuM
Oct 19, 2019 at 9:11

Since you are using LuaTeX, you can simply query \mathstyle.

\documentclass{article}
\usepackage{amsmath}
\usepackage{unicode-math}

\newcommand\catlim{%
\ifcase\mathstyle
% 0 = \displaystyle
\expandafter\varprojlim
\or
% 1 = \crampeddisplaystyle
\expandafter\varprojlim
\else
% all other styles
\expandafter\lim
\fi}

\begin{document}

Inline $$\catlim_{i \in \mathbb{N}} \mathbb{Z} / p^i \mathbb{Z}$$ and display:
\begin{equation*}
\catlim_{i \in \mathbb{N}} \mathbb{Z} / p^i \mathbb{Z}.
\end{equation*}

\end{document}


It’s not clear what you want to do when you explicitly reset the math style. The following solution, which requires amsmath but works also with the “classical” typesetting engines, discriminates based on the \if@display switch. This purposely ignores the math style (\displaystyle vs. other styles), and decides which command to use (\varprojlim vs. \lim) considering only if the formula is being displayed or typeset in-line.

I insist that it is not clear from your question whether you consider this a bug or a feature. See also @daleif’s comment.

The code:

% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly
% declare the paper format.

\usepackage[T1]{fontenc}         % Not always necessary, but recommended.
% End of standard header.  What follows pertains to the problem at hand.

\usepackage{mathtools} % also loads "amsmath"
\usepackage{amsfonts}

\makeatletter

\newcommand*\catlim{%
\relax\if@display
\expandafter\varprojlim
\else
\expandafter\lim
\fi
}

\makeatother

\newcommand*{\numberset}[1]{\mathbb{#1}}
\newcommand*{\N}{\numberset{N}}
\newcommand*{\Z}{\numberset{Z}}

\begin{document}

In line:
$$\catlim_{i\in\N} \Z/p^{i}\Z$$.

In line, but with \verb|\displaystyle|:
$$\displaystyle \catlim_{i\in\N} \frac{\Z}{p^{i}\Z}$$.

In display:
$\catlim_{i\in\N} \frac{\Z}{p^{i}\Z}$

In display, but with \verb|\textstyle|:
$\textstyle \catlim_{i\in\N} \Z/p^{i}\Z$

An example with \texttt{cases} (starred version requires the
\textsf{mathtools} package):
$G = \begin{cases*} \catlim_{i\in\N} \frac{\Z}{p^{i}\Z} & first case \\ \catlim_{i\in\N} \frac{\Z}{q^{i}\Z} & second case \end{cases*}$
Note how the fraction is typeset: this too shows that
\verb|\textstyle| is in force.

\end{document}


The printout:

• +1 but there is a point you might want to clarify: \if@display won't work with something like $\displaystyle...$. Just to be thorough. Oct 18, 2019 at 10:28
• @campa: Thank you for your suggestion; please see edit.
– GuM
Oct 18, 2019 at 22:35