What you want—automatic detection of left-to-right/right-to-left script inside a verbatim
environment—is not easily possible right now. (It could probably be done with interchar tokens in XeTeX, or Lua.) What you were trying to do was giving you mojibake gibberish.
You should always add the line \tracinglostchars=2
if you’re switching between different fonts to support different languages. That will warn you if the current font doesn’t support a glyph you asked for, which usually means you have the wrong language selected.
The following MWE almost works, in XeLaTeX or LuaLaTeX:
\tracinglostchars=2 % Warn if a glyph is missing from a font!
\documentclass{book}
\usepackage[bidi=default, layout=sectioning.counters, english]{babel}
\babelprovide[import, main, maparabic, alph=alphabetic, roman=abjad]{persian}
% The environment defined by \babeltags fails to set the text direction.
\DeclareRobustCommand\textpersian[1]{\foreignlanguage{persian}{#1}}
\newenvironment{english}%
{\begin{otherlanguage}{english}}%
{\end{otherlanguage}}
\usepackage{unicode-math}
\defaultfontfeatures{ Renderer=HarfBuzz, Scale=MatchLowercase, Ligatures=TeX }
\babelfont{rm}
{Amiri}
\babelfont{tt}
{almfixed.otf}
\begin{document}
\chapter{فصل}
\section{بخش} % text missing
یک متن % text missing
$a^{2} = b^{2} + c^{2}$
a**2 = b**2 + c**2
\begin{english}
\obeylines
00027 printf({"{}JUST A PRINT STATEMENT\(\backslash\)n"{}});
00027 printf({"{}Just a print statement\(\backslash\)n"{}});
00028 printf({"large \textpersian{یک متن} Doxygen 1.8.19\(\backslash\)n"{}});
\begin{verbatim}
00027 printf({"{}JUST A PRINT STATEMENT SOME MORE TEXT\(\backslash\)n"{}});
00027 printf({"{}Just a print statement some more text\(\backslash\)n"{}});
00028 printf({"large یک متن Doxygen 1.8.19\(\backslash\)n"{}});
\end{verbatim}
\end{english}
\tableofcontents
\end{document}

If you look very closely, you’ll see that the Persian text in the \verbatim
environment is not being displayed in the correct direction (even though I manually inserted right-to-left and left-to-right marks in the source).
You could get what you probably want by escaping out your text like in the first block and using alltt
, listing
, etc. instead of verbatim
:
\tracinglostchars=2 % Warn if a glyph is missing from a font!
\documentclass{book}
\usepackage[bidi=default, layout=sectioning.counters, english]{babel}
\usepackage{alltt}
\babelprovide[import, main, maparabic, alph=alphabetic, roman=abjad]{persian}
% The environment defined by \babeltags fails to set the text direction.
\DeclareRobustCommand\textpersian[1]{\foreignlanguage{persian}{#1}}
\newenvironment{english}%
{\begin{otherlanguage}{english}}%
{\end{otherlanguage}}
\usepackage{unicode-math}
\defaultfontfeatures{ Renderer=HarfBuzz,Scale=MatchLowercase, Ligatures=TeX }
\babelfont{rm}
{Amiri}
\babelfont{tt}
{almfixed.otf}
\setmathfont{LibertinusMath-Regular.otf}
\begin{document}
\chapter{فصل}
\section{بخش} % text missing
یک متن % text missing
$a^{2} = b^{2} + c^{2}$
a**2 = b**2 + c**2
\begin{english}
\obeylines
00027 printf({"{}JUST A PRINT STATEMENT\(\backslash\)n"{}});
00027 printf({"{}Just a print statement\(\backslash\)n"{}});
00028 printf({"large \textpersian{یک متن} Doxygen 1.8.19\(\backslash\)n"{}});
\begin{alltt}
00027 printf({"{}JUST A PRINT STATEMENT\(\backslash\)n"{}});
00027 printf({"{}Just a print statement\(\backslash\)n"{}});
00028 printf({"large \textpersian{یک متن} Doxygen 1.8.19\(\backslash\)n"{}});
\end{alltt}
\begin{verbatim}
00027 printf({"{}JUST A PRINT STATEMENT SOME MORE TEXT\(\backslash\)n"{}});
00027 printf({"{}Just a print statement some more text\(\backslash\)n"{}});
00028 printf({"large یک متن Doxygen 1.8.19\(\backslash\)n"{}});
\end{verbatim}
\end{english}
\tableofcontents
\end{document}

In theory, it’s supposed to be possible to set up XeTeX to automatically switch languages when you change scripts, wirh ucharclasses
. The manual claims that something like this might do it:
\usepackage[Arabics]{ucharclasses}
\setTransitionsForArabics%
{\begingroup\selectlanguage{persian}}%
{\endgroup}
As of 2020, the package is broken and does not seem to be actively maintained. Many of the interactions between babel
and fontspec
appear to be broken in TeX Live 2020 as well, but those I was able to work around.
I’m going to omit a solution that supports PDFTeX, since it would be completely different and consist entirely of hacks around the fact that classic 8-bit TeX was never designed to support Persian.