tud-cd / tudscr

TUD-Script
Other
106 stars 23 forks source link

3-line mathmode setting for upright indices wont work anymore #57

Closed Pratched closed 3 years ago

Pratched commented 3 years ago

I noticed, that this probably known "setting" for upright indices in mathmode without a need for single \mathrm{} every time, no longer works with tudscr. I havnt used tudscr for a while, but it worked at least 1-2 yrs ago.

\mathcode`\.="8000
\begingroup\lccode`~=`.
\lowercase{\endgroup\def~}#1{\mathrm{#1}}

Furthermore is it working when using scr itself or even article, as following MWE might prove.

\documentclass{tudscrbook}
%\documentclass{scrbook}
%\documentclass{article}
\mathcode`\.="8000
\begingroup\lccode`~=`.
\lowercase{\endgroup\def~}#1{\mathrm{#1}}
\begin{document}
$a_.b$\\
$a_.{bv}$\\
$a_b$
\end{document}

In case it is not as common as I thought, I cant remember where I first found this "setting", but it is described here as well.

mrpiggi commented 3 years ago

This is related to the usage of package mathastext in order to use Open Sans for math mode. The named package defines a large amount of tokens including . to be active.

I would suggest a slightly different approach for creating upright subscripts, inspired by https://tex.stackexchange.com/a/496613 With \uprightsubscripts and \italicsubscripts you can define, whether you want to use upright or italic subscripts as default. In order to get the contrary output, you only have to use $x_|subscript|$ instead of the normal syntax $x_{subscript}$.

\documentclass{tudscrartcl}

\makeatletter
\newcommand*\mathsub@upright[1]{\sb{\mathrm{#1}}}
\newcommand*\mathsub@italic[1]{\sb{\mathit{#1}}}
\newcommand*\mathsub@default{}
\newcommand*\mathsub@variant{}
\begingroup%
  \catcode`\_=13%
  \gdef_{%
    \kernel@ifnextchar(%)
      {\mathsub@variant}%
      {\mathsub@default}%
  }%
  \gdef_{\@ifnextchar|\mathsub@variant\mathsub@default}%
\endgroup%
\AtBeginDocument{%
  % default setting if \[upright|italic]subscripts wasn't invoked so far
  \ifx\mathsub@variant\@empty\italicsubscripts\fi%
  \mathcode`_="8000%
  \catcode`\_=12%
}
\newcommand*\uprightsubscripts{
  \let\mathsub@default\mathsub@upright%
  \def\mathsub@variant(##1){\mathsub@italic{##1}}%
  \def\mathsub@variant|##1|{\mathsub@italic{##1}}%
}
\newcommand*\italicsubscripts{%
  \let\mathsub@default\mathsub@italic%
  \def\mathsub@variant(##1){\mathsub@upright{##1}}%
  \def\mathsub@variant|##1|{\mathsub@upright{##1}}%
}
\makeatother

\begin{document}
$a_(b)$
$a_(bv)$
$a_|b|$
$a_|bv|$
$a_b$
$x_{default}$
$x_(variant)$
$x_|variant|$

\uprightsubscripts
$a_(b)$
$a_(bv)$
$a_|b|$
$a_|bv|$
$a_b$
$x_{default}$
$x_(variant)$
$x_|variant|$

\italicsubscripts
$a_(b)$
$a_(bv)$
$a_|b|$
$a_|bv|$
$a_b$
$x_{default}$
$x_(variant)$
$x_|variant|$
\end{document}

EDIT: Regarding https://github.com/tud-cd/tudscr/issues/57#issuecomment-799393041, changed from $x_|variant|$ to $x_(variant)$

keinstein commented 3 years ago

Am 15.03.21 um 12:41 schrieb Falk Hanisch:

In order to get the contrary output, you only have to use $x_|subscript|$ instead of the normal syntax $x_{subscript}$.

Why don't you suggest to use [ and ]? It is common LaTeX for optional arguments and has asymmetric borders.

mrpiggi commented 3 years ago

Well, the argument is not optional but a variant for the mandatory one. Nevertheless, asymmetric borders is a point, changed from $x_|variant|$ to $x_(variant)$

Pratched commented 3 years ago

Ah alright. Im not going to pretend, I fully understand whats been done neither in my original snippets or your solution, but many thanks.

mrpiggi commented 3 years ago

Leave open as potential feature for a future release

Pratched commented 3 years ago

That would be great. Especially since upright indices are expected most of the time throughout TUD. One last question though, since Ive been too quick in closing anyway:

This does not seem to work when using predefined symbols with glossaries when using said syumbols with \gls{}? Might there be a quick fix for that as well?

mrpiggi commented 3 years ago

please open a new issue and provide a mwe

mrpiggi commented 3 years ago

According to https://tex.stackexchange.com/q/62705 and https://tex.stackexchange.com/q/245880 a slightly different and (hopefully) more robust solution considering text mode as well

\documentclass{tudscrartcl}
\usepackage[T1]{fontenc}

\makeatletter
\newcommand*\mathsub@upright[1]{\sb{\mathrm{#1}}}
\newcommand*\mathsub@italic[1]{\sb{\mathit{#1}}}
\newcommand*\mathsub@default{}
\newcommand*\mathsub@variant{}
\newcommand*\uprightsubscripts{
  \let\mathsub@default\mathsub@upright%
  \def\mathsub@variant(##1){\mathsub@italic{##1}}%
}
\newcommand*\italicsubscripts{%
  \let\mathsub@default\mathsub@italic%
  \def\mathsub@variant(##1){\mathsub@upright{##1}}%
}
\newcommand*\mathsub@choice{%
  \kernel@ifnextchar(%)
    {\mathsub@variant}%
    {\mathsub@default}%
}
\catcode`\_=\active%
\gdef_{\relax\ifmmode\expandafter\mathsub@choice\else\textunderscore\fi}%
\AtBeginDocument{%
  % ensure right catcodes
  \catcode`\_=\active%
  \mathcode`_="8000%
  % default setting if \[upright|italic]subscripts wasn't invoked so far
  \ifx\mathsub@variant\@empty\italicsubscripts\fi%
}
\makeatother

\usepackage{hyperref}
\begin{document}
\begin{figure}
  \caption{$x_y$}
  \label{z}
\end{figure}

bar_{foo}

bar_(foo)

$bar_{foo}$

$bar_(foo)$

\bigskip

$a_(b)$
$a_(bv)$
$a_b$
$x_{default}$
$x_(variant)$

\uprightsubscripts
$a_(b)$
$a_(bv)$
$a_b$
$x_{default}$
$x_(variant)$

\italicsubscripts
$a_(b)$
$a_(bv)$
$a_b$
$x_{default}$
$x_(variant)$
\end{document}
mrpiggi commented 3 years ago

Final solution can be found here: https://github.com/tud-cd/tudscr/issues/58#issuecomment-805298549