How to color specific word in a paragraph more times?
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}
macros color paragraphs
add a comment |
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}
macros color paragraphs
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. includesdocumentclass,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
add a comment |
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}
macros color paragraphs
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}
macros color paragraphs
macros color paragraphs
edited Mar 11 at 9:00
koleygr
13k11038
13k11038
asked Mar 11 at 8:58
Zlatan RadovanovicZlatan Radovanovic
32
32
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. includesdocumentclass,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
add a comment |
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. includesdocumentclass,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
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
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
1
okay, will note that in my future posts, thanks
– Zlatan Radovanovic
Mar 13 at 22:16
okay, will note that in my future posts, thanks
– Zlatan Radovanovic
Mar 13 at 22:16
add a comment |
1 Answer
1
active
oldest
votes
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 defdokument{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")}}
newcommandstopcoloring{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:

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")}}
newcommandstopcoloring{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:

wow I haven't expected such a thorough answer, thanks, this helped a lot
– Zlatan Radovanovic
Mar 13 at 22:17
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "85"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f478851%2fhow-to-color-specific-word-in-a-paragraph-more-times%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 defdokument{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")}}
newcommandstopcoloring{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:

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")}}
newcommandstopcoloring{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:

wow I haven't expected such a thorough answer, thanks, this helped a lot
– Zlatan Radovanovic
Mar 13 at 22:17
add a comment |
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 defdokument{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")}}
newcommandstopcoloring{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:

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")}}
newcommandstopcoloring{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:

wow I haven't expected such a thorough answer, thanks, this helped a lot
– Zlatan Radovanovic
Mar 13 at 22:17
add a comment |
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 defdokument{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")}}
newcommandstopcoloring{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:

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")}}
newcommandstopcoloring{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:

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 defdokument{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")}}
newcommandstopcoloring{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:

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")}}
newcommandstopcoloring{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:

answered Mar 11 at 15:27
MarijnMarijn
8,069636
8,069636
wow I haven't expected such a thorough answer, thanks, this helped a lot
– Zlatan Radovanovic
Mar 13 at 22:17
add a comment |
wow I haven't expected such a thorough answer, thanks, this helped a lot
– Zlatan Radovanovic
Mar 13 at 22:17
wow I haven't expected such a thorough answer, thanks, this helped a lot
– Zlatan Radovanovic
Mar 13 at 22:17
wow I haven't expected such a thorough answer, thanks, this helped a lot
– Zlatan Radovanovic
Mar 13 at 22:17
add a comment |
Thanks for contributing an answer to TeX - LaTeX Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f478851%2fhow-to-color-specific-word-in-a-paragraph-more-times%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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