KL divergence (short form) in LaTeX (\text{KL}(P \| Q))

KL(P∥Q)\text{KL}(P \| Q)
LaTeX command
\text{KL}(P \| Q)
Package
Add to your preamble:\usepackage{amsmath}
Mode
Math mode only

Where it is used

Shorthand notation for the Kullback-Leibler divergence between distributions P and Q, without the D prefix.

Many machine learning papers drop the D and write \text{KL}(P \| Q), especially in loss functions like the VAE objective with a term \text{KL}(q_\phi(z|x) \| p(z)). It means exactly the same as D_{\text{KL}}(P \| Q), including the asymmetry: forward KL tends to be mass-covering, reverse KL mode-seeking. Because \text{} copies the font of surrounding text, KL can come out italic inside a theorem; \operatorname{KL} or \mathrm{KL} fixes that and \operatorname also adds the right space before the parenthesis. Consider \,\|\, or \parallel for more air around the bars.

Minimal compilable example

Paste this into an empty .tex file and compile it with pdfLaTeX, XeLaTeX or LuaLaTeX.

L=−Eq[log⁡p(x∣z)]+KL(q(z∣x)∥p(z))\mathcal{L} = -\mathbb{E}_{q}[\log p(x|z)] + \text{KL}(q(z|x) \| p(z))
\documentclass{article}
\usepackage{amssymb,amsmath}
\begin{document}
KL divergence (short form): $\text{KL}(P \| Q)$

\[ \mathcal{L} = -\mathbb{E}_{q}[\log p(x|z)] + \text{KL}(q(z|x) \| p(z)) \]
\end{document}

Related symbols

All التعلم الآلي والذكاء الاصطناعي →

Frequently asked questions

How do I write KL divergence (short form) in LaTeX?
Type \text{KL}(P \| Q) in math mode, for example $\mathcal{L} = -\mathbb{E}_{q}[\log p(x|z)] + \text{KL}(q(z|x) \| p(z))$. Load amsmath first with \usepackage{amsmath}.
Which package do I need for \text{KL}(P \| Q)?
\text{KL}(P \| Q) needs amsmath: add \usepackage{amsmath} to your preamble before the document starts.
Can I use \text{KL}(P \| Q) in text mode, or only in math mode?
Only in math mode. Outside $...$ LaTeX stops with "Missing $ inserted", so write $\text{KL}(P \| Q)$ in running text.
Math poweredاكتب LaTeX باستخدام الذكاء الاصطناعي

أفضل محرر LaTeX بالذكاء الاصطناعي، بديل Overleaf

يساعد Octree الباحثين من أكثر من 15 جامعة على كتابة أوراق البحث بشكل أسرع باستخدام الذكاء الاصطناعي

  • أنشئ رسوم TikZ جميلة باستخدام الذكاء الاصطناعي
  • ارفع صورة وحولها إلى LaTeX بسلاسة
  • احصل على مساعدة فورية في كتابة وتنسيق أوراق البحث