diff --git a/src/common/config.py b/src/common/config.py
index b19288bec56dbadfea453221bb6be63e74139586..71f05b176c3454e869f342404f9cc1e2afe1fba0 100644
--- a/src/common/config.py
+++ b/src/common/config.py
@@ -225,6 +225,7 @@ class Config:
 		'show_contacts_number': [opt_bool, True, _('If True, Gajim will show number of online and total contacts in account and group rows.')],
 		'treat_incoming_messages': [ opt_str, '', _('Can be empty, \'chat\' or \'normal\'. If not empty, treat all incoming messages as if they were of this type')],
 		'scroll_roster_to_last_message': [opt_bool, True, _('If True, Gajim will scroll and select the contact who sent you the last message, if chat window is not already opened.')],
+		'use_latex': [opt_bool, False, _('If True, Gajim will convert string between $$ and $$ to an image using dvips and convert before insterting it in chat window.')],
 	}
 
 	__options_per_key = {
diff --git a/src/conversation_textview.py b/src/conversation_textview.py
index 3e1d29cff45efb3b9d24b5bcb713799ecc34ff0b..1ba02b9d889e23ea7c1e3028be6c236b4a7a85b3 100644
--- a/src/conversation_textview.py
+++ b/src/conversation_textview.py
@@ -14,6 +14,10 @@
 ## GNU General Public License for more details.
 ##
 
+import random
+from tempfile import gettempdir
+from subprocess import Popen
+
 import gtk
 import pango
 import gobject
@@ -572,6 +576,50 @@ class ConversationTextview:
 
 		return index # the position after *last* special text
 
+	def latex_to_image(self, str):
+		result = None
+		
+		str = str[2:len(str)-2]
+	    
+		random.seed()
+		tmpfile = os.path.join(gettempdir(), "gajimtex_" + random.randint(0, 100).__str__())
+
+		# build latex string
+		texstr = "\\documentclass[12pt]{article}\\usepackage[dvips]{graphicx}\\usepackage{amsmath}\\usepackage{amssymb}\\pagestyle{empty}"
+		texstr += "\\begin{document}\\begin{large}\\begin{gather*}"
+		texstr += str
+		texstr += "\\end{gather*}\\end{large}\\end{document}"
+
+		file = open(os.path.join(tmpfile + ".tex"), "w+")
+		file.write(texstr)
+		file.flush()
+		file.close()
+
+		p = Popen(['latex', '--interaction=nonstopmode', tmpfile + '.tex'], cwd=gettempdir())
+		exitcode = p.wait()
+
+		if exitcode == 0:	    
+			p = Popen(['dvips', '-E', '-o', tmpfile + '.ps', tmpfile + '.dvi'], cwd=gettempdir())
+			exitcode = p.wait()
+
+		if exitcode == 0:
+		    p = Popen(['convert', tmpfile + '.ps', tmpfile + '.png'], cwd=gettempdir())
+		    exitcode = p.wait()
+	    
+		extensions = [".tex", ".log", ".aux", ".dvi", ".ps"]
+	    
+		for ext in extensions:
+			try:
+				os.remove(tmpfile + ext)
+			except Exception:
+				pass
+	    
+		if exitcode == 0:
+			result = tmpfile + '.png'
+	    	
+		return result
+
+
 	def print_special_text(self, special_text, other_tags):
 		'''is called by detect_and_print_special_text and prints
 		special text (emots, links, formatting)'''
@@ -656,6 +704,24 @@ class ConversationTextview:
 			else:
 				if not show_ascii_formatting_chars:
 					special_text = special_text[1:-1] # remove _ _
+		elif special_text.startswith('$$') and special_text.endswith('$$'):
+			imagepath = self.latex_to_image(special_text)
+			end_iter = buffer.get_end_iter()
+			anchor = buffer.create_child_anchor(end_iter)
+			if imagepath != None:
+				img = gtk.Image()
+				img.set_from_file(imagepath)
+				img.show()
+				# add
+				self.tv.add_child_at_anchor(img, anchor)
+				# delete old file
+				try:
+					os.remove(imagepath)
+				except Exception:
+					pass
+			else:
+				buffer.insert(end_iter, special_text)
+			use_other_tags = False
 		else:
 			#it's a url
 			tags.append('url')
diff --git a/src/gajim.py b/src/gajim.py
index cfcd4340dcad23c9f70111686a64b996aad434f7..704a1c22ba2f86673e2f5f8d1b4ea98b4e132831 100755
--- a/src/gajim.py
+++ b/src/gajim.py
@@ -1823,7 +1823,13 @@ class Interface:
 			r'(?<!\w|\<)' r'/[^\s/]' r'([^/]*[^\s/])?' r'/(?!\w)|'\
 			r'(?<!\w)' r'_[^\s_]' r'([^_]*[^\s_])?' r'_(?!\w)'
 
+		latex = r'|\$\$.*\$\$'
+		
 		basic_pattern = links + mail
+		
+		if gajim.config.get('use_latex'):
+			basic_pattern += latex
+		
 		if gajim.config.get('ascii_formatting'):
 			basic_pattern += formatting
 		self.basic_pattern_re = re.compile(basic_pattern, re.IGNORECASE)