Help understanding a list of commands to define a Hebrew environment

amsthmenvironmentshebrewtheorems

I want to define two new theorem-like environments in my LaTeX document, based on the amsthm package, to produce a Hebrew heading for an "example" environment with the word "דוגמה" (English: Example) and a question environment with the header "שאלה" (English: question).

I found on the net, in this website, a MWE that does just that, and I would like to have your help understanding its reasoning.

Code:

% !TEX TS-program = xelatex
% !TEX encoding = UTF-8 Unicode
\documentclass{article}
%\usepackage[utf8]{inputenc} % not needed since compiled with XeTeX
\usepackage{amsthm,amssymb,amsmath}
\usepackage{polyglossia}
\setdefaultlanguage[numerals=hebrew]{hebrew}
\setotherlanguage{english}

\usepackage{fontspec}
\newfontfamily\hebrewfont[Script=Hebrew]{Hadasim CLM}
\newfontfamily\hebrewfontsf[Script=Hebrew]{Miriam CLM}
\newfontfamily\hebrewfonttt[Script=Hebrew]{IBM Plex Sans Hebrew}
\newfontfamily{\englishfont}{Caladea}
\newfontfamily{\englishfontsf}{Parix}
\newfontfamily{\englishfonttt}{Merriweather}
%%% End of fonts

%%% Code downloaded from xitable
%\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Textclass specific LaTeX commands.
\theoremstyle{remark}
\newtheorem{question}{\protect\questionname}
\newtheorem{quesh}{שאלה}
\renewenvironment{question}{\begin{quesh}}{\end{quesh}}

\theoremstyle{definition}
\newtheorem{example}{\protect\examplename}

%\makeatother

  \addto\captionsenglish{\renewcommand{\questionname}{Question}}
  \addto\captionsenglish{\renewcommand{\examplename}{Example}}
  \addto\captionshebrew{\renewcommand{\questionname}{שאלה}}
  \addto\captionshebrew{\renewcommand{\examplename}{דוגמה}}
  \providecommand{\questionname}{שאלה}
  \providecommand{\examplename}{דוגמה}

\begin{document}

\section{שאלות לדיון}
\begin{english}
Some questions and examples at the end of the lecture.
\end{english}
\begin{quesh}
מהי פונקציה רציפה?
\end{quesh}
\begin{question}
מתי שני המספרים הרציונליים $\frac{a}{b}$ ו $\frac{c}{d}$ שווים זה לזה?
\end{question}
\begin{example}
$3+2\mathrm{i}$ הוא דוגמה למספר מרוכב.
\end{example}
\end{document}

Notes:

  • The above document compiled successfully with XeLaTeX in Overleaf. The above fonts are installed on their TeX distribution.
  • I didn't provide any counters for the environments, on purpose. to de-clutter the code.

My questions are:

  1. As you can see, for questions, two environments are defined. The first is question, the other is quesh. Is it just a not-good-practice of coding made by the original programmer? If not, then why is that?

  2. Is there a real need for the first lines of code to be inserted inside the \makeatletter and \makeatother? I commented-out both, but in the original file they are not commented.

  3. What are the \protect\questionname or (\protect\__environment__name in general) do? Where can I find documentation about those arguments? Perhaps in the future I would need to change also the table of contents and the appendix headers. What is the syntax for those entries?

  4. Is it possible to achieve this goal with less LaTeX commands?

  5. And most importantly, can anyone delineate the "workflow", or line-of-reasoning, for those commands that define the new environments?

Best Answer

A possible always-answer to type-5 questions is "not much" (and there are various ways that that can typically happen, most boiling down to variations of "if it runs, it runs").

Anyway, stripping out all "non-essential" code gives:

quesh

By "non-essential", I mean that I don't know what the code is supposed to do, or be for, so it is not adding any functionality (my impression is that it is "accretion" code, e.g., copy-pastes accumulating uncritically over time (and there could be valid reasons for that)).

The basic setup is clear:

Code sets Hebrew as the main language, and defines two theorem-environments (quesh and example). Since I don't know what else it is supposed to do (cannot download the .tex file in the link to find out), I commented those extraneous parts out to see what effect they had (none, that I could see).

polyglossia loads fontspec, so loading fontspec again isn't really needed.

I don't have the fonts, so I just used some random ones.

Compiled with lualatex.

Looks OK.

Note: tcolorbox package has specialties in this area and could be useful (layout, formatting, styles, etc).

MWE

\documentclass{article}
\usepackage{amsthm,amssymb,amsmath}
\usepackage{polyglossia}
\setdefaultlanguage[numerals=hebrew]{hebrew}
\setotherlanguage{english}

%\usepackage{fontspec}
\newfontfamily\hebrewfont[Script=Hebrew]{David Libre}%{Hadasim CLM}
\newfontfamily\hebrewfontsf[Script=Hebrew]{FreeSans}%{Miriam CLM}
\newfontfamily\hebrewfonttt[Script=Hebrew]{FreeMono}%{IBM Plex Sans Hebrew}
\newfontfamily{\englishfont}{Noto Serif}%{Caladea}
%\newfontfamily{\englishfontsf}{Parix}
%\newfontfamily{\englishfonttt}{Merriweather}
%%% End of fonts

%%% Code downloaded from xitable
%\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Textclass specific LaTeX commands.
\theoremstyle{remark}
%\newtheorem{question}{\protect\questionname}
\newtheorem{quesh}{שאלה}
%\renewenvironment{question}{\begin{quesh}}{\end{quesh}}
%\newenvironment{question}{\begin{quesh}}{\end{quesh}}

\theoremstyle{definition}
\newtheorem{example}{דוגמה}%{\protect\examplename}

%\makeatother

%  \addto\captionsenglish{\renewcommand{\questionname}{Question}}
%  \addto\captionsenglish{\renewcommand{\examplename}{Example}}
%  \addto\captionshebrew{\renewcommand{\questionname}{שאלה}}
%  \addto\captionshebrew{\renewcommand{\examplename}{דוגמה}}
%%  \providecommand{\questionname}{שאלה}
%%  \providecommand{\examplename}{דוגמה}

\begin{document}

\section{שאלות לדיון}
\begin{english}
Some questions and examples at the end of the lecture.
\end{english}
\begin{quesh}
מהי פונקציה רציפה?
\end{quesh}
\begin{quesh}
מתי שני המספרים הרציונליים $\frac{a}{b}$ ו $\frac{c}{d}$ שווים זה לזה?
\end{quesh}
\begin{example}
$3+2\mathrm{i}$ הוא דוגמה למספר מרוכב.
\end{example}

\begin{example}
$1+1$ הוא דוגמה למספר מרוכב.
\begin{english}Some more instructions.\end{english}
\end{example}

\end{document}

polyglossia has language definition files. If you look inside gloss-hebrew.ldf, you will see:

\def\captionshebrew{%
  \def\prefacename{מבוא}%
  \def\refname{מקורות}%
  \def\abstractname{תקציר}%
  \def\bibname{ביבליוגרפיה}%
  \def\chaptername{פרק}%
...

The \addto\captionshebrew part is designed for user-adds to this list (another way is to add lines to the/a .ldf file directly), makes the processing/coding more systematic. It depends what you want to do and how you want to solve for it.


Update 1

Regarding the question in a comment: for auto-translate (besides Davislor's solution),use a variable name as the label/title of the environment, and add the translation text to both language caption sets.

Define the label (say, xyz) as a variable (say, \xyzname):

\newcommand{\xyzname}{XYZName}

Define a new theorem environemt xyz using that label variable-name:

\newtheorem{xyz}{\xyzname}

Now, tell polyglossia what to set the variable to when changing languages:

\addto\captionsenglish{\renewcommand{\xyzname}{XYZ}}
\addto\captionshebrew{\renewcommand{\xyzname}{רשת}}

Usage:

\begin{xyz}
רשת
\end{xyz}

Note: doing \setdefaultlanguage[numerals=hebrew]{hebrew} sets numerals to Hebrew for everything; without the option (i.e., \setdefaultlanguage{hebrew}), it looks like this:

using a variable name

MWE

\documentclass{article}
\usepackage{amsthm,amssymb,amsmath}
\usepackage{polyglossia}
\setdefaultlanguage{hebrew}
\setotherlanguage{english}

\newfontfamily\hebrewfont[Script=Hebrew]{David Libre}%{Hadasim CLM}
\newfontfamily\hebrewfontsf[Script=Hebrew]{FreeSans}%{Miriam CLM}
\newfontfamily\hebrewfonttt[Script=Hebrew]{FreeMono}%{IBM Plex Sans Hebrew}
\newfontfamily{\englishfont}[Colour=blue]{Noto Serif}%{Caladea}


\theoremstyle{definition}
\newcommand{\xyzname}{XYZName}
\newtheorem{xyz}{\xyzname}
\addto\captionsenglish{\renewcommand{\xyzname}{XYZ}}
\addto\captionshebrew{\renewcommand{\xyzname}{רשת}}


\begin{document}

עברית:
\begin{xyz}
רשת
\end{xyz}

%====================

\begin{english}
English:
\begin{xyz}
xxx
\end{xyz}
\end{english}

\end{document}

Update 1.a

Theorem-numbering

Defining a theorem environment also defines a corresponding theorem-number variable, which can be added to the captions list - in this case, polyglossia's hebrew module provides a \hebrewnumeral{} function.

numbering

A theorem environment xyz has a counter xyz, whose printable version \thexyz can be added to the hebrew captions after being converted with \hebrewnumeral{\value{xyz}}:

\addto\captionshebrew{\renewcommand{\thexyz}{\hebrewnumeral{\value{xyz}}}}

MWE

\documentclass{article}
\usepackage{amsthm,amssymb,amsmath}
\usepackage{polyglossia}
\setotherlanguage{english}
\setotherlanguage{hebrew}

\newfontfamily\hebrewfont[Script=Hebrew]{David Libre}%{Hadasim CLM}
\newfontfamily\hebrewfontsf[Script=Hebrew]{FreeSans}%{Miriam CLM}
\newfontfamily\hebrewfonttt[Script=Hebrew]{FreeMono}%{IBM Plex Sans Hebrew}
\newfontfamily{\englishfont}[Colour=blue]{Noto Serif}%{Caladea}


\theoremstyle{definition}
\newcommand{\xyzname}{XYZName}
\newtheorem{xyz}{\xyzname}
\addto\captionsenglish{\renewcommand{\xyzname}{XYZ}}
\addto\captionshebrew{\renewcommand{\xyzname}{רשת}}
\addto\captionshebrew{\renewcommand{\thexyz}{\hebrewnumeral{\value{xyz}}}}

\begin{document}
%---
\begin{hebrew}
עברית:
\begin{xyz}
רשת
\end{xyz}
\end{hebrew}
%====================
\begin{english}
English:
\begin{xyz}
xxx
\end{xyz}
\end{english}
%---
\begin{hebrew}
עברית:
\begin{xyz}
רשת
\end{xyz}
\end{hebrew}
%====================
\begin{english}
English:
\begin{xyz}
xxx
\end{xyz}
\end{english}
%---
\begin{hebrew}
עברית:
\begin{xyz}
רשת
\end{xyz}
\end{hebrew}
%====================
\begin{english}
English:
\begin{xyz}
xxx
\end{xyz}
\end{english}

\end{document}
Related Question