# conditional equation in latex [duplicate]

how can I write this formula by latex • Use the cases environment from amsmath. Nov 28, 2020 at 6:34
• Welcome to TeX.SX! 😊 You can have a look at our starter guide to familiarize yourself further with our format. Please avoid "do this for me" questions, you should at least post a minimal working example (MWE) to show your efforts. Also, please mark and upvote the correct answer. Enjoy this community! Nov 28, 2020 at 7:39

One way is to use:

\begin{align}
p_{i \, j} = \begin{cases} \frac{1}{|p_{i}|} & \text{if $P_{i}$ links to $P_{j}$} \\
0 & \text{otherwise} \end{cases}
\end{align}


where align is used for spacing around the equation(s) or an inline version is:

 $$p_{i \, j} = \begin{cases} \frac{1}{|p_{i}|} & \text{if P_{i} links to P_{j}} \\ 0 & \text{otherwise} \end{cases}$$


The results can be viewed as (in their respective order): • Nice answer. An image of the output would be handy! Nov 28, 2020 at 6:38
• One minor tweak: you might want \lvert p_{i} \rvert, so as to space the absolute-value bars as left and right delimiters. Nov 28, 2020 at 6:39
• align is not intended for single equation. Nov 28, 2020 at 7:55
• Usage of  is deprecated in LaTeX. Instead it you should use $ and $ or \begin{equation} and \end{equation} (see my answer). You should provide an MWE, which reproduce showed image. And let me repeat, your use of align in the first example is wrong. Nov 28, 2020 at 21:13

With use of cases* and/or dcases?defined in the mathtools package and defining \abs{...} as pair of delimiters for absolute value:

\documentclass[12pt]{article}
\usepackage{mathtools}
\DeclarePairedDelimiter\abs{\lvert}{\rvert}

\begin{document}
\begin{equation}
p_{ij} = \begin{cases*}
\dfrac{1}{\abs{p_{i}}}   & if $P_{i}$ links to $P_{j}$ \\
0       & otherwise
\end{cases*}
\end{equation}
or
\begin{equation}
p_{ij} = \begin{dcases*}
\frac{1}{\abs{p_{i}}}   & if $P_{i}$ links to $P_{j}$ \\
0       & otherwise
\end{dcases*}
\end{equation}

\end{document} • Neat! Curious to the thought behind the \, extra space between the indices? Nov 28, 2020 at 7:41
• @Holene, oh, they are superfluous ... deleted now. Thank you very much to point me on this. Nov 28, 2020 at 7:58