Browsing Tag

colors

Latex

Bringing Colors to TeXShop

January 4, 2013

In an effort to continue posts with colors in common I want to bring up TeXShop. TeXShop is a latex editor of Mac that comes with MacTex distributions. In my mind it is the only Latex editor you will need, but what do I now. For those of you who do not know about Latex, it is a markup typeset base on Donald Knuth’s TeX typesettings. Latex has the ability to handle just about anything: math symbols, algorithms, flow charts, you name it. I have learned how to use Latex about 3 years ago, and I cannot recall a document I wrote that has not been typed in Latex. That is a whle other story for another time.

Here is a Simple Latex document I found online. I had made just available here just in case you could not find it. I also had to add comments to show you how annoying the color scheme can get. Here is a screen shot of what the file would look like with the default TexShop color scheme:

wpid-texshop_default_1-2013-01-4-07-42.png

I think this is just awful. So, lets change it. In order to change the colors you will have to run some terminal command. I know, one would think that by now you could change it through preferences. Nope. There are 7 items you could change the colors to. For each you would have to run 3 commands, one for each RGB value. The RGB values have to be in a float RGB scale. You can consult this float RGB color chart if you need. The 7 items you could color are:

background
commands
comments
foreground
index
insert_point
marker

For example, say we want to change the comments from that horrible red to something more easy on the eye. Lets go with some light purple, like <0.8, 0.0, 1.0>. The commands will be:


defaults write TeXShop commentred 0.8
defaults write TeXShop commentgreen 0.0
defaults write TeXShop commentblue 1.0

You will have to restart TeXShop and now comments will look like this:

wpid-texshop_default_2-2013-01-4-07-42.png

In a similar way you can change the rest of the colors to match you style. Here is a bash script to change all the colors on TexShop. The colors are set for the default colors. You can change any of the colors to match you style. If you want to go back to the original simply download the file and run it. I would suggest you keep a local copy of your settings just in case. In order to run the code you will have to change the permissions to 700 by running this command:


sudo chmod 700 texshop_colors.sh

Now you can change any of the colors at by running the file instead of running multiple commands.


./teschop_colors.sh

Terminal

Syntax Highlighting in the Mac OS X Terminal

January 2, 2013

In your programming career, sooner or later you will come across the command line text editor. In the beginning it will look and feel wired, but with time you will get used to it. After a while you will get used to not having the IDE and fancy auto complete. You will even get used to the fact that the syntax is not highlighted, but you do not have to. As it turns out, you can 'activate' your terminals syntax highlighting features. This will help you a lot down the road and can be applied for Mac (yes, even servers, no I do not know about Windows.

So I created a simple program in python to see what my terminal will display now if I were to open the file via vi. Here is the file content:


import time

def main():
	print 'hello'
	x = 42

main()

If I were to run the following command (assuming the file is named syntax.py):


vi syntax.py

The output:

syntax highlight off

We can do better than that. First of all navigate to your home folder. We are going to add some color highlighting to the ls function. This will make it easy to recognize file types in the command line. That is the folder that opens up when you open up the terminal. Now run the following command to create (or modify if exsiting) your .profile file.


vi .profile

If you have File Vault turned on, you might have to add sudo at the beginning of the command and type your password at the prompt. Once the file is open type 'i' to switch to vi insert mode. Then type the following into the file:


alias ls='ls -G'

After you are done typing hit the 'esc' key followed by 'wq' and enter. This will switch you out of insert mode, save (write) & quit. See An Extremely Quick and Simple Introduction to the Vi Text Editor for more information about vi. This command will add some syntax highlighting to your terminal ls function. So now your directory list will be colorful. To add the syntax highlight to vi we will need to do a little more work.

First, change the directory like so:


cd /usr/share/vim

Then open a file called vimrc using sudo. Like this:


sudo vi vimrc

This will take you back into the vi enviorment. Now you are going to go into insert mode again by pressing 'i' key. Add the following code after the line:


backspace = 2

The code to add:


set ai                  " auto indenting
set history=100         " keep 100 lines of history
set ruler               " show the cursor position
syntax on               " syntax highlighting
set hlsearch            " highlight the last searched term
filetype plugin on      " use the file type plugins

" When editing a file, always jump to the last cursor position
autocmd BufReadPost *
\ if ! exists("g:leave_my_cursor_position_alone") |
\ if line("'\"") > 0 && line ("'\"") <= line("$") |
\ exe "normal g'\"" |
\ endif |
\ endif

Save and exit again, by pressing 'esc' and 'wq'. Restart your terminal session and everything should be all colorful. If you were to go back to view your vimrc file you should see something similar to this:

vim color configure

And out python file from before should look like this when you open it:

vim python color highlight

Much better.

Note that at any point you can disable this feature by removing the added line of code. You can also disable terminal syntax highlighting by typing in the following command from within vi:

 :syntax off 
Cool Stuff

Playing around with Strings and Python

December 28, 2012

Python is my favorite language. It allows anything from high level to low level programming. It is very easy to understand. Python can be written as an algorithm that you can execute. Today, I want to show you some cool tricks on how to manipulate strings in python.

NOTE: I am running Python2.6. I highly recommend you read the Python 2.6 String documentation prior to proceeding. I suggest you also download python2.6 documentation for offline reference.

Python stores strings in it’s virtual memory as arrays. This is very useable since you can address each character individually. Strings are 0 based arrays and support reverse indexing. However, unlike arrays, strings are not mutable. Strings also support concatenation and other cool stuff. Let’s take a look at the following output:
wpid-wpid-pythonstringmanipulation-2012-12-28-01-53-2012-12-28-01-53.png
Now, lets have fun with the output and give it some more meaning. First lets ask the user for a string and print out the characters of the string each on a line. To do so we will run this code:


a_string = raw_input("Enter some text: ")
for char in a_string:
	print char

Running the program should give you the following output for the input “Hello World!”:

wpid-wpid-pythonprintcharactersofstring-2012-12-28-01-53-2012-12-28-01-53.png

Now lets use the python built in sleep function from the time library. This will delay the printout of each char by a given time. This will cause a neat effect for the user.


import time

a_string = raw_input("Enter some text: ")

for char in a_string:
	print char
	time.sleep(1)

So far it is still dull. Let’s take it a step further and have colors. There is a great library to add colors to python standard output called termcolor. I suggest you take a look at the code for termcolor. To use it we can either create a text object and color it, or use the provided cprint function. Here is the example of printing in res through standard output:


import time
import termcolor

a_string = raw_input("Enter some text: ")

for char in a_string:
	termcolor.cprint(char, "red")
	time.sleep(1)

wpid-wpid-pythonstringredcolor-2012-12-28-01-53-2012-12-28-01-53.png

Termcolor also support printing on color, blinking, bold, italic and others. There is a brief example on the termcolor web page on how to use. That should cover anything you need to know. If I get the time I will come back and write a tutorial for it. If you have questions before such time, feel free to contact the Captain. For the next one, we will print each letter in a different color, like this:


import time
import termcolor

a_string = raw_input("Enter some text: ")
i = 0

for char in a_string:
	if (i%7 == 0):
		termcolor.cprint(char, "grey")
	elif (i%7 == 1):
		termcolor.cprint(char, "red")
	elif (i%7 == 2):
		termcolor.cprint(char, "green")
	elif (i%7 == 3):
		termcolor.cprint(char, "yellow")
	elif (i%7 == 4):
		termcolor.cprint(char, "blue")
	elif (i%7 == 5):
		termcolor.cprint(char, "magenta")
	elif (i%7 == 6):
		termcolor.cprint(char, "cyan")

	time.sleep(1)
	i += 1

wpid-wpid-pythonprintingindiffrentcolors-2012-12-28-01-53-2012-12-28-01-53.png

Now what if you want to print in the same line? Well, we will need to use another standard library called sys.stdout. What we will do is suppress the carige return from the print statement by placing a comma (‘,’) at the end of the print statement. Without flushing the print statement, the output will just appear after the entire line was printed out. Since we want the suspense, we add the flush. Thus printing the input string back to standard output, one character at a time. Check it out:


import time
import termcolor
import sys

a_string = raw_input("Enter some text: ")

for char in a_string:
	print char,
	sys.stdout.flush()
	time.sleep(1)

wpid-wpid-pythonprintacharacteratatime-2012-12-28-01-53-2012-12-28-01-53.png

If we want a different color on each character and have them all on the same line we will need to use the object colored and the regular print statements like before. The code will look like this:


import time
import termcolor
import sys

a_string = raw_input("Enter some text: ")
i = 0

for char in a_string:
	if (i%7 == 0):
		print termcolor.colored(char, "grey"),
	elif (i%7 == 1):
		print termcolor.colored(char, "red"),
	elif (i%7 == 2):
		print termcolor.colored(char, "green"),
	elif (i%7 == 3):
		print termcolor.colored(char, "yellow"),
	elif (i%7 == 4):
		print termcolor.colored(char, "blue"),
	elif (i%7 == 5):
		print termcolor.colored(char, "magenta"),
	elif (i%7 == 6):
		print termcolor.colored(char, "cyan"),

	sys.stdout.flush()
	time.sleep(1)
	i += 1

The screen capture:

wpid-wpid-pythonsamelinediffretcolorcharacters-2012-12-28-01-53-2012-12-28-01-53.png

That is all for. Now you have some extra tools in you arsenal to making some neat command line programs using python. If you are really into it I suggest you take a look at the following program, it is really sci-fi.

download python string sci-fi


# written and executed on mac OSX 10.6.8 running python 2.6.8

import time
import termcolor
import sys
import random

colors = ("grey", "red", "green", "yellow", "blue", "magenta", "cyan")
attributes = ("bold", "underline", "blink", "reverse")

def get_color():
	global colors
	return colors[random.randint(0,len(colors)-1)]
	
def get_on_color():
	global colors
	return "on_" + colors[random.randint(0,len(colors)-1)]
	
def get_attributes():
	global attributes
	return attributes[random.randint(0,len(attributes)-1)]
	
for i in range(100**2):
	x = random.randint(0,6)
	the_char = chr(random.randint(33,126))
	if (x%6 == 0):
		print termcolor.colored(the_char, get_color(), get_on_color(), attrs=[get_attributes()]),
	elif (x%6 == 1):
		print termcolor.colored(the_char, get_color(), get_on_color()),
	elif (x%6 == 2):
		print termcolor.colored(the_char, get_color()),
	elif (x%6 == 4):
		print termcolor.colored(the_char, get_color(), attrs=[get_attributes()]),
	elif (x%6 == 5):
		print termcolor.colored(the_char, get_on_color(), attrs=[get_attributes()]),
	else:
		print the_char,
	
	sys.stdout.flush()
	time.sleep(0.3)


Screen capture of the string secret program - 1

Screen capture of the string secret program - 2