0
\documentclass{article}
\usepackage{graphicx} % Required for inserting images
 \newcommand{\TaI}{111222333444555. 666777888. 9990000. }
 \newcommand{TbI}{}
\begin{document}

\TaI
\end{document}

I would assign part (i.e. the first 6 charater of TaI) to variable TbI how can I otain that? How can I count the first 6 character and the select them? thank you

Renato

5

2 Answers 2

3

I guess you want to do this in a fairly general way. My suggestion is to use names rather than commands.

\documentclass{article}

\ExplSyntaxOn

% user interface
\NewDocumentCommand{\definevar}{mm}
 {
  \renatop_var_define:nn { #1 } { #2 }
 }
\NewDocumentCommand{\appendtovar}{mm}
 {
  \renatop_var_append:nn { #1 } { #2 }
 }
\NewDocumentCommand{\prependtovar}{mm}
 {
  \renatop_var_prepend:nn { #1 } { #2 }
 }
\NewDocumentCommand{\extractfromvar}{O{#2}mmm}
 {
  \renatop_var_extract:nnnn { #1 } { #2 } { #3 } { #4 }
 }
\NewExpandableDocumentCommand{\usevar}{m}
 {
  \renatop_var_use:n { #1 }
 }

% variables
\tl_new:N \l__renatop_var_tmp_tl

% internal functions
\cs_new_protected:Nn \renatop_var_define:nn
 {
  \tl_clear_new:c { l__renatop_var_#1_tl }
  \tl_set:cn { l__renatop_var_#1_tl } { #2 }
 }
\cs_new_protected:Nn \renatop_var_append:nn
 {
  \tl_put_right:cn { l__renatop_var_#1_tl } { #2 }
 }
\cs_new_protected:Nn \renatop_var_prepend:nn
 {
  \tl_put_left:cn { l__renatop_var_#1_tl } { #2 }
 }
\cs_new_protected:Nn \renatop_var_extract:nnnn
 {
  \tl_set_eq:Nc \l__renatop_var_tmp_tl { l__renatop_var_#2_tl }
  \tl_replace_all:Nnn \l__renatop_var_tmp_tl { ~ } { \__renatop_var_space: }
  \tl_set:Ne \l__renatop_var_tmp_tl
   {
    \tl_range:Nnn \l__renatop_var_tmp_tl { #3 } { #4 }
   }
  \tl_replace_all:Nnn \l__renatop_var_tmp_tl { \__renatop_var_space: } { ~ }
  \tl_clear_new:c { l__renatop_var_#1_tl }
  \tl_set_eq:cN { l__renatop_var_#1_tl } \l__renatop_var_tmp_tl
 }
\cs_new_protected:Nn \__renatop_var_space: {}
\cs_new:Nn \renatop_var_use:n { \tl_use:c { l__renatop_var_#1_tl } }

\ExplSyntaxOff

\begin{document}

\definevar{TaI}{666777888}
\prependtovar{TaI}{111222333444555 }
\appendtovar{TaI}{ 9990000 }

Show: X\usevar{TaI}X % X to show spaces

\extractfromvar[TbI]{TaI}{1}{6}

Show: X\usevar{TbI}X

\extractfromvar{TaI}{-6}{-1}

Show: X\usevar{TaI}X

\end{document}

The code shows that you can define a variable also by “accretion” (it's not mandatory, of course). The extraction command needs some explanation:

  1. the first argument is optional and defaults to the second one (which is mandatory;
  2. the second argument is the name of the variable to extract from; if the optional argument is not specified, then the variable will be set to the extracted one;
  3. the third argument is the starting point;
  4. the fourth argument is the end point;
  5. both can be negative, meaning “counting from the end”.

Since \tl_range:Nnn behaves a bit weirdly with spaces (there are several technical reasons for this), when extraction is performed spaces are temporarily replaced with a placeholder that will be changed back to a space.

enter image description here

3

enter image description here

You can use a helper macro that just uses its first 6 arguments.

\documentclass{article}
 \newcommand{\TaI}{111222333444555. 666777888. 9990000. }
 \def\tmp#1#2#3#4#5#6#7\tmp{#1#2#3#4#5#6}
 \edef\TbI{\expandafter\tmp\TaI\tmp}
\begin{document}

\TaI

\TbI

\end{document}
4
  • yes. I think this works for me. Can you explain me how does it works? I didn't understand.
    – RenatoP
    May 15 at 16:11
  • \tmp has 6 normal arguments and a 7th which is everything up to \tmp. The \expandafter means that \tmp 111222333444555. 666777888. 9990000. \tmp gets executed so the first 6 arguments are 111222 and the rest is discarded, this ignores spaces in the count so an initial string of 1 1 122 2 d dls asa would also make 111222 @RenatoP May 15 at 16:32
  • mmhhhh...still don't understand. Imagin in TaI I have a lot of words (from 100 to 1000 or more) and I would take just the first 'n' where 'n' is a number from 30 to 50 and put it in TbI. How can I do that wit your example?
    – RenatoP
    2 days ago
  • @RenatoP you can't: this method can extract between 1 and 8 items as a macro may have at most 9 parameters and you need the last parameter to discard the remaining. 2 days ago

You must log in to answer this question.

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