6

In this MWE, I demonstrate my problem:

\documentclass{article}
\usepackage{seqsplit}
\newcommand{\dosomething}[1]{\uppercase{#1}}
\newcommand{\dosomethingelse}[1]{\seqsplit{#1}}
\begin{document}
    \dosomething{looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong}

    % works with extra {...}
    \dosomething{{looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong}}

    \dosomethingelse{looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong}

    % does not work with extra {...}
    \dosomethingelse{{looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong}}
\end{document}

Some commands work with extra curly braces, some don't. Why is that, and how can I make them work by preprocessing my arguments when they do have extra curly braces?

4
  • If you remove the {...} in \dosomethingelse around {#1} it works with the doubled {...} as well.
    – user31729
    Mar 25, 2016 at 18:23
  • But then the use case without extra {...} (example 3) will stop working... (it still compiles, but it stops doing what it should, which is breaking the line).
    – bers
    Mar 25, 2016 at 18:24
  • In my point of view, the excess {....} forms a group that can't be used by the seqsplit command in order to split it
    – user31729
    Mar 25, 2016 at 18:31
  • I agree. Is there a way to ungroup this group?
    – bers
    Mar 25, 2016 at 18:31

2 Answers 2

10

If the argument is not empty and starting spaces can be removed, then the following trick helps:

\makeatletter
\newcommand{\dosomethingelse}[1]{%
  \expandafter\seqsplit\expandafter{\@firstofone#1}%
}
\makeatother

\@firstofone is defined in the LaTeX kernel as:

\long\def\@firstofone#1{#1}

It grabs the first token as argument and outputs it again, thus it does "nothing". But if the argument is not a single token, but a token group in braces, then one level of braces are removed.

2
  • Thanks! Can one make this a standalone command such as \removeBraces[1] or so? Then one could call \seqsplit{\removeBraces{#1}} - I don't seem able to put this together.
    – bers
    Mar 25, 2016 at 19:15
  • 1
    @bers \seqsplit scans the argument without expanding it. When \removeBraces is called, it's already too late.The answer uses \expandafter to expand \@firstofone (requires one expansion step exactly), before \seqsplit scans it argument. Mar 25, 2016 at 19:45
2

It mostly depend on what command you're dealing with.

If you have \textit{{xyz}}, the additional braces just add a level of grouping; for \seqsplit it's a completely different ballgame, because this command scans its argument one item at a time and a braced group is a single item.

This is described in the manual of seqsplit, in section 2.3:

2.3 Grouping and Commands

The command \seqsplit does not insert breakpoints between the letters inside braces {...}.

[...(omitted example)...]

The braces around {kahg} prevented a splitting of this group. This effect can be used for typesetting special substrings inside sequences.

Braces have a very important syntactical meaning and should not be used in a casual way.

You must log in to answer this question.

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