Can i call macros something like this


So, i would like to have running window of length 3. And for this end i would like to be able to use temporary unnamed local variable \X+2 as i can in C++.

  • 1
    Sorry, but it's very unclear what your aim is.
    – egreg
    Apr 24, 2017 at 11:18
  • @egreg Op wishes, given a string, to be able to extract and display a substring (a window into the string). This substring should be specified by its start index and length, as opposed to start/end indices, using the \StrMid command from xstring package.
    – marsupilam
    Apr 24, 2017 at 13:02

3 Answers 3


You can use \numexpr\X+2 in order to evaluate the number of the end-'index' here.

Of course \numexpr\foo+\foobar will be an even more flexible solution.

Since \StrMid is not expandable the usability of \StrMid depends on the real design.



\def\mystring{And now for something completely different}


This yields or something c here.

  • @StevenB.Segletes: ;-) You should look for many other answers of mine having MP references -- you will get a lot of upvotes to be done then :D
    – user31729
    Apr 24, 2017 at 11:12
  • 1
    It's not meant to be taken literally. [Obviously it refers to any manufacturers of dairy products.] Apr 24, 2017 at 11:16
  • @StevenB.Segletes: What a pity/shame :D
    – user31729
    Apr 24, 2017 at 11:56

A fully expandable function, with the help of expl3:


  \tl_map_function:fN { \tl_range:onn { #1 } { #2 } { #3 } } \use:n
\cs_generate_variant:Nn \tl_map_function:nN { f }
\cs_generate_variant:Nn \tl_range:nnn { o }






The second and third argument of \stringwindow can be any integer denotation, that is, also counter registers, such as \value{chapter}, besides macros that expand to numbers.

Note: spaces are not preserved. A slower routine would be needed for this.

enter image description here


Here's a LuaLaTeX-based solution. Note that the macro \StrMid is fully expandable. All three arguments of \StrMid may consist of (or contain) macros; the only requirement is that the args evaluate to either a string (the first arg) or integers (the second and third arg). And, the result of \StrMid may be assigned to a new macro.

enter image description here

% !TeX program = lualatex
\usepackage{luacode} % for "\luastring" macro


\newcommand\foo{2+2} % something that evaluates to an integer in Lua 

\StrMid{\mystring}{\foo}{\foo+2} % returns "now"

% assign result of \StrMid to a LaTeX macro:
\bbar % returns "let"

You must log in to answer this question.

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