How do I change the color of each letter in a sentence in LaTeX? - colors

I've been struggling in find the solution for the following problem.
If I want to change the color of each letter in a sentence in latex, how can I do it? Let's say that I want to write "Merry Christmas" using the colors red and green and alternating between them.
I want to define a command and use a forloop to change the color of each letter, but I don't know:
How can I find the number of letters in a sentence (excluding spaces) to give the upper limit for the forloop?
How can I define a string with the two colors to use in the forloop?
So far my silly "solution" is to do it manually, like:
\documentclass[a4paper,12pt]{article}
\usepackage[utf8x]{inputenc}
\usepackage{xcolor}
\begin{document}
\textcolor{red}{M}\textcolor{green}{e}\textcolor{red}{r}
\textcolor{green}{r}\textcolor{red}{y}\textcolor{green}{C}
\textcolor{red}{h}\textcolor{green}{r}\textcolor{red}{i}
\textcolor{green}{s}\textcolor{red}{t}\textcolor{green}{m}
\textcolor{red}{a}\textcolor{green}{s}\textcolor{red}{!}
\end{document}
From what I saw I can define an environment and use a loop to alternate between the colors, I don't have a sensilble solution, but it would be something like:
\newenvironment{redgreen}{
\forloop{WORDNUMBER}{1}{\VALUE{WORDNUMBER} < \LENGHT{WORDNUMBER}}{
\textcolor{red}{WORDNUMBER}
\textcolor{green}{WORDNUMBER + 1}
WORDNUMBER = WORDNUMBER + 2
}
}
The parts with capital letters I know won't work, I just want to give an example of what I'm willing to do. I want to find a way to get the number of letters in the sentence that I would put inside the redgreen block and somehow use the forloop to change the color of each letter in the sentence alternating between red and green. I'm not sure how the forloop works in latex, so everything in the example above may be trash.

Related

How to make an excel (365) function that recognizes different words in the same cell and changes them individually

What im working with
I have a list of product names, but unfortunately they are written in uppercase I now want to make only the first letter uppercase and the rest lowercase but I also want all words with 3 or less symbols to stay uppercase
im trying if functions but nothing is really working
i use the german excel version but i would be happy if someone has any idea on how to do it im trying different functions for hours but nothing is working
=IF(LENGTH(C6)<=3,UPPER(C6),UPPER(LEFT(C6,1))&LOWER(RIGHT(C6,LENGTH(C6)-1)))
but its a #NAME error excel does not recognize the first and the last bracket
This is hard! Let me explain:
I do believe there are German words in the mix that are below 4 characters in length that you should exclude. My German isn't great but there would probably be a huge deal of words below 4 characters;
There seems to be substrings that are 3+ characters in length but should probably stay uppercase, e.g. '550E/ER';
There seem to be quite a bunch of characters that could be used as delimiters to split the input into 'words'. It's hard to catch any of them without a full list;
Possible other reasons;
With the above in mind I think it's safe to say that we can try to accomplish something that you want as best as we can. Therefor I'd suggest
To split on multiple characters;
Exclude certain words from being uppercase when length < 3;
Include certain words to be uppercase when length > 3 and digits are present;
Assume 1st character could be made uppercase in any input;
For example:
Formula in B1:
=MAP(A1:A5,LAMBDA(v,LET(x,TEXTSPLIT(v,{"-","/"," ","."},,1),y,TEXTSPLIT(v,x,,1),z,TEXTJOIN(y,,MAP(x,LAMBDA(w,IF(SUM(--(w={"zu","ein","für","aus"})),LOWER(w),IF((LEN(w)<4)+SUM(IFERROR(FIND(SEQUENCE(10,,0),w),)),UPPER(w),LOWER(w)))))),UPPER(LEFT(z))&MID(z,2,LEN(v)))))
You can see how difficult it is to capture each and every possibility;
The minute you exclude a few words, another will pop-up (the 'x' between numbers for example. Which should stay upper/lower-case depending on the context it is found in);
The second you include words containing digits, you notice that some should be excluded ('00SICHERUNGS....');
If the 1st character would be a digit, the whole above solution would not change 1st alpha-char in upper;
Maybe some characters shouldn't be used as delimiters based on context? Think about hypenated words;
Possible other reasons.
Point is, this is not just hard, it's extremely hard if not impossible to do on the type of data you are currently working with! Even if one is proficient with writing a regular expression (chuck in all (non-available to Excel) tokens, quantifiers and methods if you like), I'd doubt all edge-case could be covered.
Because you are dealing with any number of words in a cell you'll need to get crafty with this one. Thankfully there is TEXTSPLIT() and TEXTJOIN() that can make short work of splitting the text into words, where we can then test the length, change the capitalization, and then join them back together all in one formula:
=TEXTJOIN(" ", TRUE, IF(LEN(TEXTSPLIT(C6," "))<=3,UPPER(TEXTSPLIT(C6," ")),PROPER(TEXTSPLIT(C6," "))))
Also used PROPER() formula as well, which only capitalizes the first character of a word.

How to highlight only certain part of matched regex in python

If you do echo -ne "line1aa\nline2bbb\nline3cc\n" | grep line2 --color=always you get color highlighted if regex matches..
I want to implement similar in python. The code looks like this
from termcolor import colored
for line in lines:
if re.search(regex, line)
print(colored(line, color='red'))
but prints the entire line. I want to color only highlighted like
in aaaabbbbccccddd, bbbb should only be highlighted red. The bbbb refers to regex. Here's a picture
Fairly simple, first print the prefix to your captured group, print with default color, then print the group with your desired color, and the suffix with default.
Refer to color guide here: How to print text with multiple different types of colors in he same line in Python 3?
Edit:
To expand on this, in your if statement you can extract the capture group. Not sure how to highlight on phone so apologies for the lack of markup but:
match = re.search().group()
you can then split the string using
values = line.split(match)
this will get you the prefix as the first member of the array (values[0]) and the suffix as the second.
Now, if there is more than 1 regex match, you will get an exception using .group(), and your line will be split into more than 2 values. To handle this, use line.split(match, 1) to only split line in 2 parts (everything after first match will be a suffix) , and group(1) to capture the first occurrence only (you need this for splitting).

How to highlight in Vim sequences of two symbols?

For more comfortable work with YAML/Ansible I wanna highlight pairs of spaces with different colors in a row.
For example where we write " - name" (six spaces here in the beginning of string) first pair of spaces will be yellow, next two will be red and so on.
But I can't understand how to write it in my .vimrc. Someone can help?
These aren’t exactly the two-space highlighting your looking for, but vim-indent-guides and indentLine both can get you the essential feature of highlighting columns of indent.
The alternative I think would be to create n match groups where group i matches 2 spaces (after the first 2(i-1) spaces), and then color those differently.

Is there a way to use the Find and Replace function in Excel to replace the first and last entry in a string of text?

I have a sequence of a letters, in my case part of a gene. I want to change the first and the last letter in this string of text, but keep the internal characters the same.
For example, if I have the the sequence:
ATCGAATCCATGACG
And I want to change the first letter, in this case A to the word START and change the last letter, G, to STOP all while keeping the internal A's and G's the same. Is this possible to do with the Find and Replace function, or will I have to write a script?
It is easy to do when I have a handful of sequences, I do it by hand. When I get into the hundreds, it can be very difficult.
Thank you.
The function LEN(text) returns the number of characters within a string of letters. MID(text, start, num_chars) returns the middle section of a string. CONCATENATE(text1, text2, ...) pieces together different strings. We can use these in combination to get what you want:
=CONCATENATE("START", MID(A1,2,LEN(A1)-2), "STOP")
You could use replace, and focus on the left and right side independently, then combine, or you can use left/right to add string of text to the available string minus a character, like:
="START"&LEFT(RIGHT(A1,LEN(A1)-1),LEN(A1)-2)&"STOP"
I used left/right, but mid would also work
just another option:
=REPLACE(REPLACE(A1,LEN(A1),1,"STOP"),1,1,"START")

Macro to compare between two columns

I need to compare two columns and find out what is missing in one of them.
Color available color required color missing
Blue, red (blue,white) or (blue,green)
White (green) or (red)
Blue,white,green (blue,white,pink) or (x,y,z)
I have a list of around 300 rows that I need to work on on a daily basis, which is very time consuming.
Can someone please help me get this information under column color missing?
This is not the type of question that will get answered here. Firstly, you are very unclear what you seek. Secondly, I suspect you will need a lot of code to achieve your objective.
Stack Overflow is for programmers seeking help to improve their code. It is not a free-coding site.
This is my guess of your requirement
Color available color required color missing
Blue, red (blue,white) or (blue,green) (white) or (green)
White (green) or (red) (green) or (red)
Blue,white,green (blue,white,pink) or (x,y,z) pink or (x and y and z)
As far as I know Stack Exchange, the parent site, does not have a site for help with designing programs which I believe is what you need.
You must first design your program. What tasks must be performed to achieve your objective? You then need to attempt to program each of those tasks in VBA. If you fail at a particular task, post your faulty code here with an explanation of its objective. If a task is small and you do not have a clue where to start, a question here might be answered.
First question for you: how much VBA do you know? There seems to be a belief that you can scan the internet for bits of VBA script and stitch then together into working program with minimal knowledge of VBA. I do not share this belief. I have tried searching the internet for answers to what I think are simple questions. I have found answers that I know include the required code but I cannot see how you could extract that code from the noise without VBA knowledge. You can search the internet for "Excel VBA Tutoral" or you can find a book in a good library or bookshop. Spend some time getting a basic understanding of VBA. That investment in time will repay itself quickly.
Below is my attempt at an initial list of tasks.
You imply the number of rows varies from day to day. You need to discover the last used row today. You need to know how to address a particular worksheet cell so you can access its value. You can access worksheet cells directly but it is faster and usually more convenient to copy the worksheet to an array. This is all basic worksheet access information.
In column 1, you have colours (some with capital letters) separated by comma or comma space. To process these colours you need then in an array:
Avail(0) = blue, Avail(1) = red
Avail(0) = white
Avail(0) = blue, Avail(1) = white, Avail(2) = green
The function Split will split strings like these into an array containing the values I have shown: Avail = Split(Col1Value, ",") Function Trim will remove leading and trailing spaces. Function LCase will convert upper case characters to lower case.
Column 2 is more complicated. In all your examples, you have a single " or " and the colour groups are surrounded by brackets. Can there be more than one " or "? Are the groups always surrounded by brackets? Could the spaces around the "or" be omitted so an example could be: (red, green)or(blue,white) or (red, green)or (blue,white)?
If the values in column 2 are inconsistent, I suspect the easiest solution is to make them consistent. For example:
Col2Value = Replace(Col2Value, ")or(", ") or (")
Col2Value = Replace(Col2Value, ") or(", ") or (")
Col2Value = Replace(Col2Value, ")or (", ") or (")
You could then use Split: Alternative = Split(Col2Value, " or ") to give:
Alternative(0) = (blue,white)
Alternative(1) = (blue,green)
For each alternative, you need to trim any spaces, remove the leading and closing brackets and split it like the Col1Value. You now have two arrays and you can search the second for values missing from the first and build a list of missing colours.
I hope this is enough for you to understand what I believe you must do. I have split the total program into small tasks. Gaining access to the worksheet. Splitting column 1 values into separate colours. Making column 2 values consistent if necessary. Splitting Column 2 values into alternatives. Splitting alternatives into required colours. Each of these is a straightforward task which you should be able to solve with relatively simple VBA.
Good luck.

Resources