# Line break in macro

I have defined a macro in my document as

\newcommand{\FSeq}[2]{\ensuremath #1_1, #1_2, \ldots,  #1_#2}


The problem is that surpasses the margin usually.

Is there anyway to solve this problem? I tried some of the other responses solution without success. Thanks in advance.

EDIT: Minimal example

\documentclass{article}
\newcommand{\FSeq}[2]{\ensuremath #1_1, #1_2, \ldots,  #1_#2}
\begin{document}
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla aaaaa  $\FSeq{R}{r}$ bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
\end{document}

-
A first not so good solution would be to add \allowbreak command after each , in your macro in order to allow TeX to break line after each comma, which is not optimal but better. After you have to decide whether you prefer TeX to place your sequence on a new line when it surpasses the margin (and have a blank space to fill on the previous line) or if you prefer to break the expression in the middle and try to optimize the break with some stretching or whatever. – Ludovic C. Jul 16 '13 at 9:45
Please add a minimal working example (MWE) It is hard to understand what #1 and #2 shall be and what you want to acomplish – Martin - マーチン Jul 16 '13 at 9:53
@Martin: #1 is (surely obviously) the term "base" in the series, #2 the number of elements. of course it doesn't work as written, since \ensuremath is being used as a modal command, but in fact it takes an argument. – wasteofspace Jul 16 '13 at 10:23
@wasteofspace I understood that so far. Given #1 = 999 and #2 = 1000it does not break any margin in my document. Of course the command should be \newcommand{\FSeq}[2]{\ensuremath{#1_1, #1_2, \ldots, #1_{#2}}} – Martin - マーチン Jul 16 '13 at 10:37
@Martin The problem appears when you call this new command and you are already at the very end of a line, then it does not break the line and surpasses the margins. – Ludovic C. Jul 16 '13 at 10:47

I'd avoid an automatic breaking, offering a choice for allowing it in places where no other trick works.

\usepackage{xparse}
\NewDocumentCommand{\FSeq}{smm}{%
\IfBooleanTF{#1}
{#2_{1},\allowbreak #2_{2},\dots,\allowbreak #2_{#3}}
{#2_{1}, #2_{2},\dots, #2_{#3}}%
}


Thus $\FSeq{x}{n}$ won't allow breaks, but in case of need just adding a * will, with $\FSeq*{x}{n}$.

Example document:

\documentclass[draft]{article} % draft makes overfull hboxes visible

\usepackage{xparse}
\NewDocumentCommand{\FSeq}{smm}{%
\IfBooleanTF{#1}
{#2_{1},\allowbreak #2_{2},\dots,\allowbreak #2_{#3}}
{#2_{1}, #2_{2},\dots, #2_{#3}}%
}

\begin{document}

bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla aaaaa $\FSeq{R}{r}$ bla bla bla bla bla bla bla bla
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla

bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla aaaaa $\FSeq*{R}{r}$ bla bla bla bla bla bla bla bla
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla

\end{document}


If, for some reasons, the xparse package is not available, the old method for defining command with a *-variant can be used:

\makeatletter
\DeclareRobustCommand{\FSeq}{\@ifstar\olmos@sfseq\olmos@fseq}
\newcommand{\olmos@sfseq}[2]{%
#1_{1},\allowbreak #1_{2},\dots,\allowbreak #1_{#2}%
}
\newcommand{\olmos@fseq}[2]{%
#1_{1}, #1_{2},\dots, #1_{#2}%
}
\makeatother


Alternatively, with two macros instead of three,

\makeatletter
\DeclareRobustCommand{\FSeq}{%
\@ifstar{\@tempswatrue\olmos@fseq}{\@tempswafalse\olmos@fseq}}
\newcommand{\olmos@fseq}[2]{%
#1_{1},\if@tempswa\allowbreak\fi #1_{2},\dots,%
\if@tempswa\allowbreak\fi #1_{#2}%
}
\makeatother

-

Improvement but not a solution

You can add some \allowbreak commands in your macro which will look like this

\newcommand{\FSeq}[2]{\ensuremath{#1_1,\allowbreak #1_2,\allowbreak \ldots,\allowbreak #1_#2}}


Why is this better? This gives TeX the possibility to break your expression in the middle after a , if it is crossing the margin.

Why isn't it a solution? Because you may still experience some parts of your expression to cross the margin if TeX cannot adapt the space between the words of the line so that it does not cross (See the picture below).

Besides it isn't good to split a mathematical expression on two lines...

"Manual" solution

You can go through your document after compilation using the .log file to track down the Overfull \hbox warnings and adapt your .tex file where it crosses the margin. Generally a good idea is to use  to display your expression (or thedmath environment from the breqn package). However in your case the macro you defined is to quote a sequence (I guess from the name and the output) which may seem awkward to print with such an environment...

The Solution then would be to rephrase the paragraph where you experience the Overfull \hbox warning.

To an automatic solution?

I am currently trying to develop a script which automatically detect the Overfull \hbox warning and adapt the .tex file after multiple compilations to treat them and place the mathematical expression into a dmath environment instead of the inline mode (See my related question).

I will update this answer if I get some insight for this solution...

-