LaTeX error "Extra \fi": cause and fix
TeX found a \fi that closes nothing, because no \if... condition is open at that point. Extra \else and Extra \or are the same message for the two other parts of a condition. TeX ignores the command and goes on.
Why it happens
- The
\iffalsein front of a block was deleted, and its\fistayed behind. - The
\if...command is not defined, for example\ifdraftwithout\newif\ifdraft, or\ifpdfwithout its package. The log reports Undefined control sequence first, thenExtra \elseandExtra \fi. - A definition of your own contains one
\fimore than it has conditions.
How to fix it
- Delete the
\fior\elsein the line the log names, or restore the\if...that belongs to it. - Declare a switch of your own before you use it:
\newif\ifdraft, then\drafttrueor\draftfalse. - For
\ifpdf,\ifluatexand\ifxetex, load theiftexpackage.
A document that produces the message
\documentclass{article}
\begin{document}
Visible text.
This paragraph was switched off before.
\fi
More visible text.
\end{document}The corrected document
\documentclass{article}
\begin{document}
Visible text.
This paragraph was switched off before.
More visible text.
\end{document}Both documents were compiled with pdfLaTeX from TeX Live 2023. The first produces the message, the second compiles without an error.
Finding the line in the editor
The editor lists every error and warning of a compile run with file and line. Select an entry and the cursor jumps to that line. See Error list with jump to the line.