# How to colour text inside aligned gb4e gloss examples?

I am using the package gb4e to gloss a number of linguistic examples. Its main feature, is that it aligns the words in the original language with glosses.

I would like to be able to change the font/colour of some parts of the text so as to highlight those that are more important in a particular context.

Example. In the following example, the first line is how I currently imagine my ideal solution. Unfortunately, it does not work. The second line, instead, is my current solution: this works, but I have to repeat the command for every gloss and makes the source code less readable. The third line is a failed attempt to dodge the issue: grouping together several words doesn't work because it makes them part of the same gloss.

\documentclass{report}
\usepackage{color}
\usepackage{gb4e}

\begin{document}

\begin{exe}
\ex \glll
a \color{blue} b c d \color{black} e f g\\
a {\color{blue} b} {\color{blue} c} {\color{blue} d} e f g\\
a {\color{blue} b c d} e f g\\

\end{exe}

\end{document}


output:

I am looking for a better solution which achieves the same result as the second line of the given example, but it does not make the source code so unreadable.

I am open to suggestions which do not involve the package gb4e, in case there exist better packages for dealing with this kind of situations.

edit: the ideal solution is something of the form

a \fromNowOnTextIsBlue b c d \fromNowOnTextIsBlackAgain e f g\\


that is, a way to change the colour "globally", so that it spans beyond the local scope. In fact, the package gb4e considers any command as a standalone gloss and thus it does not propagate its effect across subsequent words. Is that possible?

• You should probably just define a \newcommand. – AML May 25 '18 at 14:30
• For each and every word? @AML – Patrick Trentin May 25 '18 at 14:38
• You just want to turn random letters/word blue, right? – AML May 25 '18 at 14:39
• Yes, but a very large and diverse set of words which may appear both coloured and not. – Patrick Trentin May 25 '18 at 14:41
• Is your goal to not have to type \color{blue} because it's a bit long? Because you can define a \newcommand to shorten it. I have an answer ready that does it. – AML May 25 '18 at 14:42

Here's a version which patches the cgloss4e glossing macros to add the colour of your choice. I've created three macros:

\glosscolor{} % sets the colour of the gloss elements
\clt  % turn colouring on
\clf  % turn colouring off


Since this modifies the glossing parser code, it will work with either \gll or \glll lines. It will not work in regular example lines. The colouring turns off at the end of each gloss line. This uses the xcolor package instead of the color package, so that the color changes can be made relative to the current document color.

\documentclass{report}
\usepackage{xcolor} % instead of the color package
\usepackage{gb4e}

\makeatletter
\newcommand*\glosscolor[1]{\gdef\@glcolor{#1}}
\glosscolor{red}
\AtBeginDocument{\colorlet{savedcol}{.}}
\def\@glosscolor{savedcol}

\newcommand{\clt}{\gdef\@glosscolor{\@glcolor}\color{\@glosscolor}}
\newcommand{\clf}{\gdef\@glosscolor{savedcol}\color{\@glosscolor}}

\gdef\getwords(#1,#2)#3 #4\\% #1=linebox, #2=\each, #3=1st word, #4=remainder
\unvbox#1%
}%
\def\more{#4}%
\ifx\more\empty\let\more=\donewords
\else\let\more=\getwords
\fi
\more(#1,#2)#4\\%
}
\gdef\donewords(#1,#2)\\{\gdef\@glosscolor{savedcol}}
\makeatother

\begin{document}

\begin{exe}
\ex \glll
\clt a  b c d \clf e f g\\ % colour from a-d
a  \clt b  c  d f \clf g\\ % colour from b-f
a \clt b c d \clf e f g\\  % colour from b-d
\end{exe}

\end{document}


• In view of TeX's parsing rules, you can define \clt without an argument, and write e.g. a \clt b c d f \clf g \\  for the second row -- the second unit will be parsed as if it was written{\clt b}. Fewer tokens, and logically better since the color doesn't only affect the "argument" of \clt. Or you could define \clt to take a color argument (but then it must be attached to the following word, e.g. \clt{red}wordb :-) – alexis May 26 '18 at 7:07
• @alexis You're right. The syntax I had reflected an earlier version of the code which used \textcolor and therefore required an argument. I've updated the answer. – Alan Munn May 26 '18 at 13:28
• This answer is exactly what I was looking for, thank you. And thanks to you too, @alexis , for the suggestions you provided. – Patrick Trentin May 26 '18 at 14:57
• I posted a follow-up to this question here. I would be glad if you could take a look at that. Sorry I can't offer much more than my gratitude. :-) – Patrick Trentin May 27 '18 at 13:39

If you want to shorten \color{blue} to make your code more readable, then you can define a \newcommand to change it to something shorter, in this case I chose \cb, and you still get the same output that you wanted in the second line.

\documentclass{report}
\usepackage{color}
\usepackage{gb4e}

\newcommand{\cb}{\color{blue}}

\begin{document}

\begin{exe}
\ex \glll
a \color{blue} b c d \color{black} e f g\\
a {\cb b} {\cb c} {\cb d} e f g\\
a {\color{blue} b c d} e f g\\

\end{exe}

\end{document}


• You're replicating the undesirable behavior of misaligning the columns... The b's, c's etc. should all be on the same column like the a's. – alexis May 26 '18 at 7:13
• @alexis the alignment was not the question nor the focus of my answer. Maybe he wanted it like that; not for me, or you, to judge. Sometimes multiple words fall under a single word in the gloss above. I do it all the time. – AML May 26 '18 at 11:38
• @alexis the alignment was part of the question, as you correctly stated, but the solution he proposed does not hinder the alignment. The misalignment in his solution comes from the 3rd row which is part of the example in my question. – Patrick Trentin May 26 '18 at 14:53
• For the record, your original question asks only about coloring and not alignment. You use an alignment package, but that isn't what your question is about. Your question is 100% about coloring. The alignment part is trivial and easily corrected but is not the topic of concern here. – AML May 26 '18 at 17:39
• Ok I see, the solution is not problematic-- the illustration was just poorly chosen. But @AML, the question is about coloring that does not screw up the alignment, so the alignment of the output is certainly relevant. – alexis May 26 '18 at 18:10