0

Maybe the title is a bit misleading and it's my pilot post on here :)

Anyhow, I have a paragraph with ,let's say 100 words, from which 10 of those words are word "dokument". I have to make a command that colors that word in that paragraph, but I can't seem to get it work, I'm including part of my code down below and I hope someone can debug it and help me. :)

\begin{center}
\fontsize{17pt}{2}\selectfont TASK 1
\end{center}

\begin{center}
\begin{minipage}[b]{70mm}

\bojenje{dokument}

Ovo je primjer centriranog \textbf{paragrafa} u okruženju
minipage čija je širina 70 mm. Unutar dokumenta
svaka instanca riječi dokument je obojena plavom bojom.
Možda da ne bi bilo loše napraviti komandu koja boji riječ
dokument plavom bojom?
Naslov dokumenta je font visine 17 pt.

\end{minipage}
\end{center}
  • 1
    Welcome to tex.sx. It's a little easier to start playing with your example and help you if you include a code-snipped that is compilable (i.e. includes \documentclass, \begin{document}, etc. and a minimal preable – sheß Mar 11 at 9:03
  • 1
    okay, will note that in my future posts, thanks – Zlatan Radovanovic Mar 13 at 22:16
1

This is not really possible with pdflatex or xelatex. See the related questions automatic word in color, how to auto bold a keyword?, Is it possible to have certain words in the document always in bold?, where the advice is to define a macro (here that would be something like \def\dokument{\textcolor{blue}{dokument}} and then do search and replace in your editor to change all occurrences of dokument into \dokument (of course you can also skip the macro and search and replace dokument with \textcolor{blue}{dokument}). This would indeed be the easiest solution.

However, it can be done with LuaTeX, as noted in Is it possible to have certain words in the document always in bold?. Things have changed a bit since 2011 so here is an updated version. The idea is to register a callback for processing the lines in your document, and use this callback to replace every occurrence of your target word with the same word as argument to \textcolor{blue}.

The simplest version with the word hardcoded and coloring for the whole document:

\documentclass{article}
\usepackage{luacode}
\usepackage{xcolor}
\begin{luacode}
    function translate(line)
        return string.gsub(line, "dokument", "\\textcolor{blue}{dokument}")
    end
    luatexbase.add_to_callback("process_input_buffer", translate, "autocolor")
\end{luacode}
\begin{document}
Ovo je primjer centriranog \textbf{paragrafa} u okruženju
minipage čija je širina 70 mm. Unutar dokumenta
svaka instanca riječi dokument je obojena plavom bojom.
Možda da ne bi bilo loše napraviti komandu koja boji riječ
dokument plavom bojom?
Naslov dokumenta je font visine 17 pt.
\end{document}

For a bit of flexibility you can use some regular expression syntax to make sure the word is surrounded by word boundaries and to allow suffixes (here for example to color also the a in dokumenta. You can also activate and deactivate the callback with a separate command to be able to switch the automatic coloring on and off. Furthermore, you can make the replacement word a variable instead of hardcoding it in the gsub call.

MWE:

\documentclass{article}
\usepackage{luacode}
\usepackage{xcolor}
\begin{luacode}
    local myword = "init"

    local gsub = string.gsub
    function translate(line)
        wordpattern = "%f[%a]" .. myword .. "%a*%f[%A]"
        return gsub(line, wordpattern, "\\textcolor{blue}{%1}")
    end

    function setword(newword)
        myword = newword
    end
\end{luacode}

\newcommand{\startcoloring}[1]{\directlua{setword("#1")}%
\directlua{%
luatexbase.add_to_callback("process_input_buffer", translate, "autocolor")}}

\newcommand\stopcoloring{\directlua{%
luatexbase.remove_from_callback("process_input_buffer", "autocolor")}}

\begin{document}
The word dokument is not colored.\\

\startcoloring{dokument}
Ovo je primjer centriranog \textbf{paragrafa} u okruženju
minipage čija je širina 70 mm. Unutar dokumenta
svaka instanca riječi dokument je obojena plavom bojom.
Možda da ne bi bilo loše napraviti komandu koja boji riječ
dokument plavom bojom?
Naslov dokumenta je font visine 17 pt.\\
\stopcoloring

Again dokument is not colored.
\end{document}

Result:

enter image description here

To make it even more general you can create a list of target words (called table in Lua) and loop over all of them. Code:

\documentclass{article}
\usepackage{luacode}
\usepackage{xcolor}
\begin{luacode}
    wordlist = {}

    local gsub = string.gsub
    function translatelist(line)
        for _, word in ipairs(wordlist) do
            wordpattern = "%f[%a]" .. word .. "%a*%f[%A]"
            line = gsub(line, wordpattern, "\\textcolor{blue}{%1}")
        end
        return line
    end

    function addword(newword)
        table.insert(wordlist,newword)
    end 
\end{luacode}

\newcommand{\startcoloring}[1]{\directlua{%
luatexbase.add_to_callback("process_input_buffer", translatelist, "autocolor")}}

\newcommand\stopcoloring{\directlua{%
luatexbase.remove_from_callback("process_input_buffer", "autocolor")}}

\begin{document}
The word dokument is not colored.\\

\directlua{addword("dokument")}
\directlua{addword("instanca")}
\startcoloring{}
Ovo je primjer centriranog \textbf{paragrafa} u okruženju
minipage čija je širina 70 mm. Unutar dokumenta
svaka instanca riječi dokument je obojena plavom bojom.
Možda da ne bi bilo loše napraviti komandu koja boji riječ
dokument plavom bojom?
Naslov dokumenta je font visine 17 pt.\\
\stopcoloring

Again dokument is not colored.
\end{document}

Result:

enter image description here

  • wow I haven't expected such a thorough answer, thanks, this helped a lot – Zlatan Radovanovic Mar 13 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.