1

I want a \doifmode for text mode or a \doifnotmode for math mode but math and text are not defined as system modes by ConTeXt.

Hungarian typography requires small space before certain characters, including colons. I want to adapt that. However I want to avoid that for non-text modes.

Unfortunately I can not write a conditional code based on text/math mode which is rather frustrating.

Below is my ConTeXt MkIV code. It gives M0H0S0 for all 4 test cases. I would expect M0H1S0, M0H1S1, M1H0.., M1H0...

\mainlanguage[hu]

% adapted from magyar.ldf (LaTeX Babel)
\def\thinspaced#1{%
  % Dat: `!' at the end of the word is OK, but at the beginning it makes the
  %      word unhyphenatable. Imp: maybe \nobreak\hskip\z@skip?
  % Dat: this is math-mode safe, because \ifhmode is false in math mode
  \doifmode{*hu}{
  % THE COMMENTED CODE SHOULD WORK BELOW
  %\ifmmode% \ifhmode and \ifmmode are never true
  %  \ifdim\lastskip>\dimexpr0pt\relax
  %    \unskip\penalty\@M\kern.1em% \thinspace is .16667em
  %  \else
  %    \kern.1em
  %  \fi
  %\fi
  % TEMPORARY MODE TEST CODE BELOW
  \ifmmode\/M1\else\/M0\fi
  \ifhmode\/H1\else\/H0\fi
  \ifdim\lastskip>\dimexpr0pt\relax\/S1\else\/S0\fi
  }%
  \string#1
}

\catcode`\:=\active \edef:{\thinspaced:}

\starttext
Test1: Test1

Test2 :Test2

Mathtest1 \math{20:30} Mathtest 1

Mathtest2
\startformula
20:30
\stopformula
Mathtest2

\stoptext

Note that I just started to use ConTeXt, have no deep TeX coding knowledge, used LaTeX before but that is not good enough for my typographic needs.

Thanks in advance!

EDIT: Henri's answer pointed out that \ifhmode and \ifmmode works as intended; the problem was with \edef above.

5
  • 1
    To insert a small space before certain punctuation marks (:, ;, ! and ?) add \setcharacterspacing [frenchpunctuation] to your document. Jun 15, 2017 at 18:16
  • Thanks, I missed this setting. Unfortunately it puts explicit space character before the marks instead of kerning which also shows up in copypasted texts. But for now, it will do just fine. Still I would be courious how I would know inside that function whether I am in text mode. I am going to look at \setcharacterspacing source code. Jun 15, 2017 at 19:56
  • @SzieberthAdam It actually doesn't put exlicit spaces. It inserts glue nodes which are then probably converted to spaces by the PDF viewer's heuristic. I'm not sure. Jun 16, 2017 at 3:00
  • @SzieberthAdam You don't have to edit the answer into the question. Most people know how to scroll down ;) Jun 16, 2017 at 3:04
  • I was unsatisfied with the width of the frenchpunctation space, defined my own punctation with only 0.1em spaces and now there are no more extra spaces before the marks in the resulting PDF. Everything works as intended. Thanks guys! Jun 16, 2017 at 3:17

1 Answer 1

3

If you \edef: you are freezing it to the mode which was active at the point of definition which is vertical mode. For the definition to adapt dynamically, use \def.

\mainlanguage[hu]

% adapted from magyar.ldf (LaTeX Babel)
\def\thinspaced#1{%
  % Dat: `!' at the end of the word is OK, but at the beginning it makes the
  %      word unhyphenatable. Imp: maybe \nobreak\hskip\z@skip?
  % Dat: this is math-mode safe, because \ifhmode is false in math mode
  \doifmode{*hu}{
  % THE COMMENTED CODE SHOULD WORK BELOW
  %\ifmmode% \ifhmode and \ifmmode are never true
  %  \ifdim\lastskip>\dimexpr0pt\relax
  %    \unskip\penalty\@M\kern.1em% \thinspace is .16667em
  %  \else
  %    \kern.1em
  %  \fi
  %\fi
  % TEMPORARY MODE TEST CODE BELOW
  \ifmmode\/M1\else\/M0\fi
  \ifhmode\/H1\else\/H0\fi
  \ifdim\lastskip>\dimexpr0pt\relax\/S1\else\/S0\fi
  }%
  \string#1
}

\catcode`\:=\active \def:{\thinspaced:}

\starttext
Test1: Test1

Test2 :Test2

Mathtest1 \math{20:30} Mathtest 1

Mathtest2
\startformula
20:30
\stopformula
Mathtest2

\stoptext

enter image description here

2
  • Thanks a lot! I borrowed that catcode line from some other place. So \ifhmode and \ifmmode works as intended. Jun 16, 2017 at 2:59
  • 3
    Note that the typical way to define such commands in ConTeXt is to define a text mode command, say \textthinspaced and define a math mode command, say \maththinspaced, and then use \def\thinspaced{\mathortext\maththinspaced\textthinspaced}.
    – Aditya
    Jun 16, 2017 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.