Saturday, January 17, 2015

I wrote a clock!

So, I wrote a clock.
I based it on an analogue clock I had written for a tomato timer app (still working on that), but I added a color variation by the time, to make it cleaner. Writing the clock itself was just an exercise in Canvas, and the source code is all up there for everyone to browse. The hardest part to write was probably the color selection function, but it's tricky to make a function that's smooth, but also visually pleasing. I don't know if I've hit on a great one, but I think this one looks alright. It varies the red color based on the hour, the green based on the minutes and the blue based on the seconds, though it tweens a lot so that the transition is more smooth. I hope you like it!

Hy flow macros.

While working on my new web framework Horn, written in Hy of course, I found myself lacking some flow controls. So I just wrote some macros!

;; Additional flow macros

(defmacro/g! guard [&rest args]
  (setv g!cond (car args))
  (setv g!body (car (cdr args)))
  (setv g!rest (cdr (cdr args)))
  (if g!rest
  `(if ~g!cond
       ~g!body
       (guard ~@g!rest))
  `(if ~g!cond
       ~g!body)))

(defmacro/g! switch [variable &rest args]
  (setv g!comp (car args))
  (setv g!body (car (cdr args)))
  (setv g!rest (cdr (cdr args)))
  (setv g!cond `(~(car g!comp) ~variable ~@(cdr g!comp)))
  (if g!rest
      (if (cdr g!rest)
        `(if ~g!cond ~g!body (switch ~variable ~@g!rest))
        `(if  ~g!cond ~g!body ~@g!rest))
      `(if  ~g!cond ~g!body)))

As you can see, switch is basically just a guard statement, but with the original variable playing a role in every comparison, and with an additional else clause (since guard can only do that with a True).
It's amazing how powerful macros are, and I'm really starting to like what I can do with them. I've already submitted these to hy.contrib, and I hope they get accepted. Try some Hy!

Tuesday, January 13, 2015

Framed theorems in Latex

I just added framed theorems to my Latex repertoire. I've been using amsthm for marking theorems for a long time, but since I encountered notes that had colored frames around the theorems, I decided that I'd add them to my latex header file. The part of the header file that relates to theorems is now is thus:

\usepackage{amsthm}
\usepackage[framemethod=tikz]{mdframed}
\renewcommand{\qedsymbol}{\textbf{Q.E.D}}

\mdfdefinestyle{theoremstyle}{%
roundcorner=5pt, % round corners, makes it friendlier
leftmargin=1pt, rightmargin=1pt, % this helps with box warnings
hidealllines=true, % even friendlier
align = center, %
}

\theoremstyle{plain}
\newtheorem*{setn}{Setning}
\newtheorem*{hsetn}{Hjálparsetning}

\theoremstyle{definition}
\newtheorem*{skgr}{Skilgreining}
\newtheorem*{daemi}{Dæmi}
\newtheorem*{frumsenda}{Frumsenda}
\newtheorem*{lausn}{Lausn}


\theoremstyle{remark}
\newtheorem*{ath}{Athugasemd}
\newtheorem*{innsk}{Innskot}

\surroundwithmdframed[style=theoremstyle,backgroundcolor = green!20]{skgr}
\surroundwithmdframed[style=theoremstyle,backgroundcolor=blue!10]{setn}
\surroundwithmdframed[style=theoremstyle,backgroundcolor = orange!20]{daemi}
\surroundwithmdframed[style=theoremstyle,backgroundcolor = red!20]{ath}

(All my notes are in Icelandic, of course, This allows me to mark my theorems, my definitions, remarks and examples specially in the TeX, and to spot them easily, which is a lot of help when using the notes in open book exams.
First I tried using mdtheorem and newmdtheoremenv, but neihter would give me what I want (couldn't get it to be unnumbered without ntheorem, and that messed up the amsthm,... etc), so the ultimate solution was just to not use the built in features of mdtheorem, and just saying directly what I wanted (a colored box around my theorems). I hope this is useful to others.