Why doesn't “\vphantom” work in section titles?

For reasons of my own (so please don't advise me to simply not do that, I considered that already) I want to have this expression in the title of a section

\section{$\left< a \vphantom{L} \right| \left. b\vphantom{L} \right>$}


but this gives me

! Incomplete \iffalse;


Any idea why that happens? And maybe a workaround?

The command \vphantom is fragile. You need to protect it:

\section{$\left< a \protect\vphantom{L} \right| \left. b\protect\vphantom{L} \right>$}


However, there's a better way to do it:

\documentclass{article}
\begin{document}

\section{$\left< a \protect\vphantom{L} \right| \left. b\protect\vphantom{L} \right>$}

\section{$\langle a \mid b \rangle$}

\section{$\left< a \;\middle|\; b\right>$}

\end{document}


The first is your code, the second is what I'd prefer, because \left and \right are really doing nothing. The third uses \middle. As you notice, the first has the spacing wrong.
{"ft_lang_label":"__label__en","ft_lang_prob":0.9203059,"math_prob":0.88391954,"size":318,"snap":"2019-26-2019-30","text_gpt3_token_len":90,"char_repetition_ratio":0.11146497,"word_repetition_ratio":0.0,"special_character_ratio":0.254717,"punctuation_ratio":0.0952381,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96632797,"pos_list":[0,1,2],"im_url_duplicate_count":[null,3,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-06-21T00:18:20Z\",\"WARC-Record-ID\":\"<urn:uuid:2e8733dc-63d1-43d1-9a00-cbd5c5ace3d7>\",\"Content-Length\":\"139133\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:5e885c04-48e3-4ec4-97ff-48362489f671>\",\"WARC-Concurrent-To\":\"<urn:uuid:3da2f8ff-5481-4ca1-8541-dd5ef28f557a>\",\"WARC-IP-Address\":\"151.101.65.69\",\"WARC-Target-URI\":\"https://tex.stackexchange.com/questions/155805/why-doesnt-vphantom-work-in-section-titles/155806\",\"WARC-Payload-Digest\":\"sha1:HAUIRRDFDDSKJ7LCGF5B7RMEO4Z2SA2T\",\"WARC-Block-Digest\":\"sha1:JVXZTS4N6FR3VIE3GG6A36QGBIBZZFXJ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-26/CC-MAIN-2019-26_segments_1560627999291.1_warc_CC-MAIN-20190620230326-20190621012326-00455.warc.gz\"}"}