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:
-
As you can see, for questions, two environments are defined. The first is
question
, the other isquesh
. Is it just a not-good-practice of coding made by the original programmer? If not, then why is that? -
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. -
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? -
Is it possible to achieve this goal with less LaTeX commands?
-
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:
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
andexample
). 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
loadsfontspec
, so loadingfontspec
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
polyglossia
has language definition files. If you look insidegloss-hebrew.ldf
, you will see: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
):Define a new theorem environemt
xyz
using that label variable-name:Now, tell
polyglossia
what to set the variable to when changing languages:Usage:
Note: doing
\setdefaultlanguage[numerals=hebrew]{hebrew}
sets numerals to Hebrew for everything; without the option (i.e.,\setdefaultlanguage{hebrew}
), it looks like this:MWE
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.A theorem environment
xyz
has a counterxyz
, whose printable version\thexyz
can be added to the hebrew captions after being converted with\hebrewnumeral{\value{xyz}}
:MWE