0

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?

1
  • 1
    I've recently written a generic answer for how to understand a particular piece of code. See link. // in particular \protect is defined in section 12.11 of LaTeX2e documentation.
    – user202729
    Nov 8, 2021 at 1:05

3 Answers 3

1

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}
6
  • Thanks very much for your answer. You pointed out this ldf file. I see that it has a translation to the proof (environment?). So, for example, how can I "load", or use, this pre-defined translation for the \begin{proof} environment header? Moreover, I want to add a תרגיל environment to my document via the amsthm package (\newtheorem{exercise}{Exercise} command). How can I define the translation to exercise to be תרגיל by using \addto\captionshebrew? Could you write down a short example?
    – tush
    Nov 28, 2021 at 17:59
  • @tush For myself, I don't know (yet); Ask a separate question(s) so more eyes are on it, in particular by those who have knowledge of the matter. Generally, polyglossia and babel automatically change the captions over for you when you select a language; moreover, the ams... and related packages provide powerful and extensive ways of defining and customizing theorem (and other) environments - also, see tcolorbox package. In the meantime, I will do some exploring and see what I can find. Having a general mechanism to change adhoc captions would be handy.
    – Cicada
    Nov 30, 2021 at 7:47
  • ( 1) For proof environment, just invoke the environment (\begin{proof}...\end{proof}) - the header is automatically translated into Hebrew when Hebrew language is active. (2) Defining \newtheorem{exercise}{תרגיל} makes תרגיל the header; do you need other language versions as well?
    – Cicada
    Nov 30, 2021 at 8:22
  • You are totally right. One can simply do \newtheorem{exercise}{תרגיל} to make this header. But now let me ask this: can I define \newtheorem{exercise}{Exercise} and then define its translation to be תרגיל via captionshebrew?I tried \gappto\captionshebrew{\providecommand{\exercisename}{תרגיל}}and\gappto\captionshebrew{\def\exercisename{תרגיל}} but it doesn't work. This way I should not be worried about defining theorem environments two times. In the English part of the document I would make english the active language and for its Hebrew part the captionshebrew would get into work.
    – tush
    Nov 30, 2021 at 9:54
  • 1
    @tush See updated answer.
    – Cicada
    Dec 1, 2021 at 4:42
3

I haven't investigated the site you've linked, but think that a reasonable attempt to address your questions can be made without that.

One key consideration is the apparent intention to produce a multilingual document rather than one using just one language.

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?

I interpret this as an attempt to allow questions in both Hebrew and English. It makes sense to have different environments for the different language elements. What is not clear is the attempt to equate them; there are other, better ways to deal with the differences.

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.

\makeatletter and \makeatother are required only when a command name within the relevant scope contains an @ sign. There is no @ sign used here, so they are unnecessary, and probably better omitted to avoid confusion.

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?

In some situations, when a string specified by a command name is reused for a different purpose, such as in a running head or the table of contents, it is necessary to prevent that use from being expanded too soon, as that may cause it to be rendered incorrectly in the final output. That is the function of \protect.

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

I believe that should be possible. When using amsthm, I would simply define a \newtheoremstyle for the elements to be rendered in the alternate language, forcing the desired language to be part of the environment definition, and then use that to define another environment with a distinct name. How to define a \newtheoremstyle is described in the amsthm user guide (texdoc amsthdoc).

(La)TeX has many interesting capabilities, but automatic translation from one language to another isn't among them; when writing a document, the author almost certainly knows what language is being used for a particular passage.

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

Other than the desire to use two different languages in contexts that appear to be parallel, I can't figure out the need to define these environments in the manner shown.

Of course, I may have misunderstood the situation completely, in which case I will be happy to delete this answer.

1

You ask a number of questions here, but one the other answers haven’t addressed yet is that it is possible to write a single theorem environment that changes languages automatically. Here is one I did for Hebrew and English a while back.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .