Re: how to calculate number of characters in argument?
- From: Alan Ristow <ristow@xxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sun, 20 Jan 2008 14:16:49 -0500
Gernot Hassenpflug wrote:
Hi,
I am doing my first calculations with arguments and string lengths in
LaTeX, and would like to have available a command for calculating the
number of characters in the argument of a \newmakeboxcommand.
(For information, this is a development of CJK package for trying to
get smething like grid mode in vertical typesetting and accommodating
the use of ASCII test within CJK text).
Whether this is the best way I do not yet know but for now I am using
either \hphantom{some text} to set the length or using a newlength
\widthCJKtxt to do so as follows:
\settowidth{\widthCJKtxt}{一二三四五}\makebox[\widthCJKtxt][l]{Compare: }
1 2 3 4 5 123456789
This allows me to use space for 5 full-width CJK chars (with glue set
to 0) for typesetting 9 half-width characters (which are I believe set
in LR mode without CJK typesetting rules) and have the whole thing
embedded in a rotated minipage.
Now I would like a new command that gets used something like:
\makeASCIIbox{ASCII text here}
And within the macro for the command there is something like this:
\newlength{\boxwidth}
\newlength{\widthCJKchar}
\def\numchars % make a variable to hold number of ASCII chars
\setlength{\widthCJKchar}{\widthof{<a single CJK char here>}}
\setlength{\widthof}{\widthof{<a single ASCII char>}}
\setlength{\boxwidth}{\numchars\widthCJKchar}
How could I get the number of chars of the argument of my new command,
so that I can write perhaps something like:
\setlength{\boxwidth}{\numchars{#1}\widthCJKchar} % and \divide\by\2
Here's a snippet of code I've used for counting the number of characters after a decimal point. I can't promise it's the *best* way to do it, but thus far it works in every case I've tried. Perhaps you can modify it for your own purposes.
This code requires eTeX. It works by first stripping off everything before the decimal point, then feeding the remaining string of characters to a recursive macro that creates a \numexpr of the form:
\numexpr 0+1+1+1+1 \relax
where a "+1" is appended to the expression for each character in the string. When the end of the string is reached (detected by appending \empty to the end), the \numexpr is evaluated. Thus,
\def\test{9.5634}
\ct@countdec\test\@nil
should expand to 4.
I can't promise this is the *best* way to accomplish this, but it seems to work well for my purposes. If you don't want to use eTeX I imagine it wouldn't be too difficult to replace the \numexpr with a counter.
Alan
(Where I've written "required comment" you can instead eliminate the line break, but I find the code easier to read as I've formatted it.)
% Define utility functions for detecting the number of digits after a
% decimal point.
%
% Usage: \ct@countdec<val>\@nil
% Returns number of decimal places in <val>.
%
\def\ct@countdec#1#2\@nil{% required comment
\ifx#1\empty
0% required comment
\else
\ifx#1.% required comment
\ct@countdec@i{#2}% required comment
\else
\ct@countdec#2\empty\@nil
\fi
\fi
}
\def\ct@countdec@i#1{% required comment
\number\numexpr 0
\ct@length@i#1\empty
}
\def\ct@length@i#1{
\ifx#1\empty\empty
\expandafter\relax
\else
\ct@length@ii{+1 \ct@length@i}% required comment
\fi
}
\def\ct@length@ii#1\fi{\fi#1}
.
At the moment I am studying the calc package for pointers. And the
TeXbook for \advance and \multiply.
Many thanks,
Gernot
- Follow-Ups:
- Re: how to calculate number of characters in argument?
- From: Gernot Hassenpflug
- Re: how to calculate number of characters in argument?
- References:
- how to calculate number of characters in argument?
- From: Gernot Hassenpflug
- how to calculate number of characters in argument?
- Prev by Date: Re: Biblatex: aliases for citations, different bibstyles in a document and preamble
- Next by Date: Re: Graphics in plain tex
- Previous by thread: how to calculate number of characters in argument?
- Next by thread: Re: how to calculate number of characters in argument?
- Index(es):
Relevant Pages
|