Biggest depth of a character in a string
I don't know about your approach using a loop, but to get the depth, just use \settodepth
See : https://tex.stackexchange.com/a/37294/116936
You can also use the calc
package \depthof
\documentclass{article}
\begin{document}
\def\mystring{abcdefgh}
\newlength\myLength
\settodepth{\myLength}{\mystring}
\the\myLength
\end{document}
Cheers,
The answer by marsupilam covers how one would normally achieve the task: typeset the content and get the box depth.
There are two issues with the code you've got. First, the TeX primitive \dimen
has to be followed by a register number, not by an =
. Using the fact that \dimen0
is a scratch register in LaTeX, you therefore can use \dimen0=<some valid dimension>
. The second is that \StrMid
doesn't generate a letter per se (it's not 'expandable'), it is instructions to do that in a typesetting context. However, there is an optional argument that will 'return' what you want
\StrMid{\mystring}{\X}{\X}[\tmp] % \tmp now expands to the substring
We can use that with \expandafter
to get what \fontchardp
requires: the character number you are interested in
\documentclass{article}
\usepackage{xstring}
\begin{document}
\def\mystring{abcdefgh}
\StrLen{\mystring}[\len]
\newcount\X
\X=\len
\loop
\StrMid{\mystring}{\X}{\X}[\tmp]
\dimen0=\fontchardp\expandafter\font\expandafter`\tmp
\edef\x{\tmp\space\the\dimen0 }%
\show\x
\advance \X by -1 %
\unless\ifnum \X<1 %
\repeat
\end{document}
(I've show rather than typeset the result)
Similar to Joseph’s, but using less code
\documentclass{article}
\usepackage{xstring}
\newcount\X
\newdimen\stringdepth
\begin{document}
\def\mystring{abcdefgh}
\StrLen{\mystring}[\len]
\X=0
\stringdepth=0pt
\loop
\ifnum\len>\X
\advance\X by 1
\StrChar{\mystring}{\X}[\tmp]%
\begingroup\edef\x{\endgroup
\dimen0=\fontchardp\font`\tmp\relax
}\x
\ifdim\dimen0>\stringdepth \stringdepth=\dimen0 \fi
% just for showing the work
\typeout{\tmp\space has depth \the\dimen0 }%
\repeat
\typeout{Max depth: \the\stringdepth}
\end{document}
Of course
\settodepth{\stringdepth}{\mystring}
is much more efficient.
A different loop, quite flexible, as shown by the examples: the current item is referred to as #1
in the final argument to \stringloop
:
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\stringloop}{smm}
{
\IfBooleanTF{#1}
{ % we have a macro as argument
\tl_map_inline:Nn #2 { #3 }
}
{ % an explicit token list
\tl_map_inline:nn { #2 } { #3 }
}
}
\ExplSyntaxOff
\newdimen\stringdepth
\begin{document}
\def\mystring{abcdefgh}
\stringloop{abcdefgh}{\typeout{#1 has depth \the\fontchardp\font`#1}}
\stringdepth=0pt
\stringloop*{\mystring}{%
\ifdim\fontchardp\font`#1>\stringdepth
\stringdepth=\fontchardp\font`#1\relax
\fi
}
\typeout{Max depth in \mystring: \the\stringdepth}
\end{document}
Here is the output on the terminal (and log file):
a has depth 0.0pt
b has depth 0.0pt
c has depth 0.0pt
d has depth 0.0pt
e has depth 0.0pt
f has depth 0.0pt
g has depth 1.94444pt
h has depth 0.0pt
Max depth in abcdefgh: 1.94444pt