Showing posts with label LaTeX. Show all posts
Showing posts with label LaTeX. Show all posts

Thursday, March 19, 2026

I created a number of cheat sheets for a class I did this quarter. The cheat sheets cover the topic of metabolic control. They include: 
  1. General Metabolic Control 
  2. Advanced Metabolic Control 
  3. Linear Chains 
  4. Branch Points 
  5. Moiety Conserved Cycles
They are available a this GitHub repo:


Both the original LaTeX and pdfs files are provided.


Sunday, December 11, 2022

Experimenting with foreach loops in TikZ to draw biochemical pathways

Here is an example of a foreach loop in TikZ that can be used to draw an arbitrary long linear chain of reactions.

By setting the value of N in the following TikZ code, you can get any length linear chain. Obviously, you're limited by the width of the page. It uses the xifthen package to provide a conditional that is used to print out the last species, which is X1. There could be a better way of doing this, but this works. For example, use newcommand instead of def
 
\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{arrows}
\usetikzlibrary{calc}
\usepackage{xifthen}

\begin{document}


\begin{tikzpicture}[>=latex', node distance=2cm]     
\node (X0) {$X_o$};
\pgfmathsetmacro{\N}{4}
\foreach \x in {0,...,\N}
{
  \pgfmathtruncatemacro{\nextval}{\x+1}
  \ifthenelse{\x = \N}
        {\def\speciesName{$X_1$}}
        {\def\speciesName{\large $x_\nextval$}}
  
  \node [right of = X\x] (X\nextval) {\speciesName};   
  \draw [->,ultra thick,blue] (X\x) -- node[above, black] {$v_{\nextval}$} (X\nextval);
}
\end{tikzpicture}

\end{document}

Here are some examples for N = 0, N = 2 and N = 4



Tuesday, August 16, 2022

Plot of 1/x using pgfplots

I noticed I couldn't find pgfplots version of the $1/x$ plot that describes $e$. I needed one, so here is the LaTeX I used.

\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepgfplotslibrary{fillbetween}

\begin{document


\begin{tikzpicture}
\begin{axis}[
ytick = {1,2},
xtick = {0,1,2,3},
xlabel=$x$, ylabel=$y$,
ymax=2.5, xmin=0, ymin=0,
xlabel style = {anchor=north east},
ylabel style = {anchor=north east}
]
\addplot [color = blue, name path=A,domain=0:4, line width = 1.4pt, samples=200] {1/x};
\path [name path=B]
    (\pgfkeysvalueof{/pgfplots/xmin},0) --
    (\pgfkeysvalueof{/pgfplots/xmax},0);
\addplot [blue!20]
    fill between [of=A and B,
                  soft clip={(1,0) rectangle (2.71,25)},];
\node at (axis cs:1.5,1.2) {$y=1/x$};
\node at (axis cs:1.6,0.31) {Area = $1$};
\node at (axis cs:2.71, 0.82) {$e$};
\addplot[-latex] coordinates
           {(2.71,0.75) (2.71,0.45)};
\end{axis}
\end{tikzpicture}

\end{document} 



Thursday, September 3, 2020

Mathpix Snip

Originally Posted on  by hsauro

I came across this amazing tool that can convert images of math equations into LaTeX format. The tool can be found at https://mathpix.com/. I’ve tried it on a number of texts including some not so clear and it does a fantastic job of converting to LaTeX. Here is a screen showing part of a page from Paul’s Online Notes:



The way it works is you select the screen icon on the mathpix tool, the entire screen goes black and white, then we draw a square around the section we want to convert and that’s it. In this case, it generates the following latex

We’ll start with finding the derivative of the sine function. To do this we will need to use the definition of the derivative. It’s been a while since we’ve had to use this, but sometimes there just isn’t anything we can do about it. Here is the definition of the derivative for the sine function.
$$
\frac{d}{d x}(\sin (x))=\lim _{h \rightarrow 0} \frac{\sin (x+h)-\sin (x)}{h}
$$
since we can’t just plug in $h=0$ to evaluate the limit we will need to use the following trig formula on the first sine in the numerator.
$$
\sin (x+h)=\sin (x) \cos (h)+\cos (x) \sin (h)
$$
Doing this gives us,
$$
\begin{aligned}
\frac{d}{d x}(\sin (x)) &=\lim _{h \rightarrow 0} \frac{\sin (x) \cos (h)+\cos (x) \sin (h)-\sin (x)}{h} \\
&=\lim _{h \rightarrow 0} \frac{\sin (x)(\cos (h)-1)+\cos (x) \sin (h)}{h} \\
&=\lim _{h \rightarrow 0} \sin (x) \frac{\cos (h)-1}{h}+\lim _{h \rightarrow 0} \cos (x) \frac{\sin (h)}{h}
\end{aligned}
$$
As you can see upon using the trig formula we can combine the first and third term and then factor a sine out of that. We can then break up the fraction into two pieces, both of which can be dealt with separately.

Which when processed by LaTeX becomes:



This is rendered inside the mathpix tool but you’ll notice there isn’t a significant difference between the original and the converted image. I’ve converted some fairly rough images and it generally succeeds. It also gives you a confidence level on how well it thinks it’s done. It took under a second to generate the LaTeX.

As a harder test, I decided to attempt to translate a page from Jim Burns’ thesis. This is a thesis from the 1970s that was typed and the equations a combination of typed characters and hand drawn. The following image shows page 93 which is part of the proof for the connectivity theorem.



And here is the image analyzed by mathpix. Remarkably the conversion is almost perfect, the equations, in particular, are translated almost without error, even getting the subscripts on the subscripts correct. It got the delta F1 wrong at the start and it interpreted a mark on the paper as an apostrophe. I tried other pages that included derivatives and these converted without incident.



Thursday, October 5, 2017

Multilayered Cascade using TikZ

 Originally Posted on  by hsauro

A summer student working in my lab, Ming Hong Lui from Hong Kong University (HKUST), worked on the perturbation analysis of signaling cascades and in his writeup he use TikZ to draw a nice cascade diagram which I present here.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{arrows}

\begin{document}

\begin{tikzpicture}[>=latex',node distance = 2cm]
\node (S1) {$S_1$};
\node [right of = S1] (S2) {$S_2$};
\node [right of = S1, node distance = 1.8cm] (X1) {};
\node [below of = X1] (S3) {$S_3$};
\node [right of = S3] (S4) {$S_4$};
\draw [->,very thick] (S1) to [bend left=40] node[above] {$v_1$} (S2);
\draw [->,very thick] (S2) to [bend left=40] node[below] {$v_2$} (S1);
\draw [->,very thick] (S3) to [bend left=40] node[above] {$v_3$} (S4);
\draw [->,very thick] (S4) to [bend left=40] node[below] {$v_4$} (S3);
\node [right of= S3, node distance = 1cm] (X2){};
\node [above of= X2, node distance = 1cm] (X3){};
\draw [->,very thick] (S2) to [bend left=20] (X3);
\node [right of= S4, node distance = 0.8cm] (X4){};
\node [below of= X4, node distance = 1cm] (X5){};
\draw [->,very thick] (S4) to [bend left=20] (X5);
\node [right of = S3, node distance = 1.8cm] (X3_5) {};
\node [below of = X3_5] (S5) {$S_5$};
\node [right of = S5] (S6) {$S_6$};
\node [right of = S5, node distance = 1.8cm] (X5) {};
\node [below of = X5] (S7) {$S_{2n-1}$};
\node [right of = S7] (S8) {$S_{2n}$};
\draw [->,very thick] (S5) to [bend left=40] node[above] {$v_5$} (S6);
\draw [->,very thick] (S6) to [bend left=40] node[below] {$v_6$} (S5);
\draw [->,very thick] (S7) to [bend left=40] node[above] {$v_{2n-1}$} (S8);
\draw [->,very thick] (S8) to [bend left=40] node[below] {$v_{2n}$} (S7);
\node [right of= S7, node distance = 1cm] (X6){};
\node [above of= X6, node distance = 1cm] (X7){};
%\node at ($(S6)!.5!(X7)$) {$\ddots$};
\draw [dashed, ->,very thick] (S6) to [bend left=20] (X7);
\node [right of = S1, node distance = 1cm] (T1) {$T_1, J_1$};
\node [right of = S3, node distance = 1cm] (T2) {$T_2, J_2$};
\node [right of = S5, node distance = 1cm] (T3) {$T_3, J_3$};
\node [right of = S7, node distance = 1cm] (T4) {$T_n, J_n$};
\end{tikzpicture}

\end{document}

This code draws the following figure:




Saturday, October 15, 2016

Tikz Code for Drawing Metabolic Feedback Loops

Originally Posted on  by hsauro

I needed some figures that displayed a variety of different negative feedback loops so I created these using Tikz. Nothing particularly special. There are some absolute distances in the code which perhaps could be removed to make it more generic.



 

\documentclass{article}

\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usetikzlibrary{calc}

\begin{document}

\begin{tikzpicture}[>=latex', node distance=2cm]
\node (Xo) {};
\node [right of = Xo] (S1) {\Large $x$};
\node [right of = S1] (S2) {};

\draw [->,ultra thick,blue] (Xo) -- node[above, black] {$v_1$} (S1);
\draw [->,ultra thick,blue] (S1) -- node[above, black] {$v_2$} (S2);

% Lets draw a line with a blunt end, -|
\draw [-|,ultra thick,blue]
% start in the middle of S2, and move down 2.75 mm
% ($ ... $) notation is used to add the coordinates
($ (S1) + (0mm,-2.75mm) $)

% Now draw the line down by 3mm
% -- means draw to, + means move by
-- +(0,-3mm)
% Now move back to the left of S2
% The symbol -| means draw horizontal then vertical.
% If we used -- instead the line would be drawn
% diagonally to the reaction edge.
% (S1) is the center of the node. But we want the
% blunt end to end below the S1 line and
% half way to the left. The 10mm is half the node
% distance of 2cm, and 1mm is slightly below
% the reaction line.
-| ($ (S1) - (10mm,1mm) $);
\end{tikzpicture}

\vspace{1cm}

\begin{tikzpicture}[>=latex', node distance=2cm]
\node (Xo) {};
\node [right of = Xo] (x1) {\Large $x_1$};
\node [right of = x1] (x2) {\Large $x_2$};
\node [right of = x2] (x3) {};

\draw [->,ultra thick,blue] (Xo) -- node[above, black] {$v_1$} (x1);
\draw [->,ultra thick,blue] (x1) -- node[above, black] {$v_2$} (x2);
\draw [->,ultra thick,blue] (x2) -- node[above, black] {$v_3$} (x3);

% Lets draw a line with a blunt end, -|, using the following coords
\draw [-|,ultra thick,blue]
% start in the middle of x2, and move down 2.75 mm
% ($ ... $) notation is used to add the coordinates
($ (x2) + (0mm,-2.75mm) $)

% Now draw the line down by an additional 3mm
% -- means draw to, + means move by
-- +(0,-3mm)

% Now move back to the left of x2
% The symbol -| means draw horizontal then vertical.
% If we used -- instead the line would be drawn
% diagonally to the reaction edge.
% (S1) is the center of the node. But we want the
% blunt end to end below the S1 line and
% half way to the left. The 10mm is half the node
% distance of 2cm, and 1mm is slightly below
% the reaction line.
-| ($ (x1) - (10mm,1mm) $);
\end{tikzpicture}

\vspace{1cm}

\begin{tikzpicture}[>=latex', node distance=2cm]
\node (Xo) {};
\node [right of = Xo] (x1) {\Large $x_1$};
\node [right of = x1] (x2) {\Large $x_2$};
\node [right of = x2] (x3) {\Large $x_3$};
\node [right of = x3] (x4) {};

\draw [->,ultra thick,blue] (Xo) -- node[above, black] {$v_1$} (x1);
\draw [->,ultra thick,blue] (x1) -- node[above, black] {$v_2$} (x2);
\draw [->,ultra thick,blue] (x2) -- node[above, black] {$v_3$} (x3);
\draw [->,ultra thick,blue] (x3) -- node[above, black] {$v_4$} (x4);

% Lets draw a line with a blunt end, -|
\draw [-|,ultra thick,blue]
($ (x3) + (0mm,-2.75mm) $)
-- +(0,-3mm)
-| ($ (x1) - (10mm,1mm) $);
\end{tikzpicture}

\vspace{1cm}

\begin{tikzpicture}[>=latex', node distance=2cm]
\node (Xo) {};
\node [right of = Xo] (x1) {\Large $x_1$};
\node [right of = x1] (x2) {\Large $x_2$};
\node [right of = x2] (x3) {\Large $x_3$};
\node [right of = x3] (x4) {\Large $x_4$};
\node [right of = x4] (x5) {};

\draw [->,ultra thick,blue] (Xo) -- node[above, black] {$v_1$} (x1);
\draw [->,ultra thick,blue] (x1) -- node[above, black] {$v_2$} (x2);
\draw [->,ultra thick,blue] (x2) -- node[above, black] {$v_3$} (x3);
\draw [->,ultra thick,blue] (x3) -- node[above, black] {$v_4$} (x4);
\draw [->,ultra thick,blue] (x4) -- node[above, black] {$v_5$} (x5);

% Lets draw a line with a blunt end, -|
\draw [-|,ultra thick,blue]
($ (x4) + (0mm,-2.75mm) $)
-- +(0,-3mm)
-| ($ (x1) - (10mm,1mm) $);
\end{tikzpicture}

\vspace{1cm}

\begin{tikzpicture}[>=latex', node distance=2cm]
\node (Xo) {};
\node [right of = Xo] (x1) {\Large $x_1$};
\node [right of = x1] (x2) {\Large $x_2$};
\node [right of = x2] (x3) {\Large $x_3$};
\node [right of = x3] (x4) {};

\draw [->,ultra thick,blue] (Xo) -- node[above, black] {$v_1$} (x1);
\draw [->,ultra thick,blue] (x1) -- node[above, black] {$v_2$} (x2);
\draw [->,ultra thick,blue] (x2) -- node[above, black] {$v_3$} (x3);
\draw [->,ultra thick,blue] (x3) -- node[above, black] {$v_4$} (x4);

% Lets draw a line with a blunt end, -|
\draw [-|,ultra thick,blue] (10mm, -8mm)
-- +(0,6mm);
\node (x) at (10mm,-11mm) {\Large $x$};
\end{tikzpicture}

\end{document} 

Sunday, December 29, 2013

Converting Excel Tables to LaTeX Tables

Originally Posted on  by hsauro

Today I had the problem of converting an Excel table of values into a LaTeX table. I could have done it by hand but the table had over 400 entries so that would have been a bit tedious. Instead I did the usual and hunted around for any automated possibilities and came across two:

1. Excel2LaTeX.

This is a Excel Add-In that converts a selected set of Excel rows and columns into a LaTeX table. However as per usual for Microsoft it isn’t trivial to add Add-Ins. After some fuzzing around I discovered these instructions on stackoverflow. One would think that adding an Add-In to Excel would be a case of simply loading the Add-In file, but of course not, it requires a minimum of at least 8 different steps. But it doesn’t end there. The final step, number 9, is to close Excel, yes I said close Excel, restart Excel and the Add-In is available (hopefully). After that the Add-In is fairly trivial to use and offers a number of useful formating options..

ExcelToLatex1

2. The second option is an online tool by Eric Wood This was very easy to use, just drag your excel file on the the browser window. If your data is on a particular sheet, make sure to select that sheet on the provided dropdown list.

ExcelToLatex2


Converting Excel Tables to LaTeX Tables

December 29, 2013 6:31 pm

Today I had the problem of converting an Excel table of values into a LaTeX table. I could have done it by hand but the table had over 400 entries so that would have been a bit tedious. Instead, I did the usual and hunted around for any automated possibilities and came across two:

1. Excel2LaTeX.

This is a Excel Add-In that converts a selected set of Excel rows and columns into a LaTeX table. However as per usual for Microsoft it isn't trivial to add Add-Ins. After some fuzzing around I discovered these instructions on stackoverflow. One would think that adding an Add-In to Excel would be a case of simply loading the Add-In file, but of course not, it requires a minimum of at least 8 different steps. But it doesn't end there. The final step, number 9, is to close Excel, yes I said close Excel, restart Excel and the Add-In is available (hopefully). After that, the Add-In is fairly trivial to use and offers a number of useful formatting options..


2. A second option is an online tool by Eric Wood This was very easy to use, just drag your excel file on the browser window. If your data is on a particular sheet, make sure to select that sheet on the provided dropdown list.



Thursday, September 12, 2013

How to Increase MikTeX 2.9 Memory

Originally Posted on  by hsauro

I was recently running some large stochastic simulations and was generating very large pgfplot data files. MikTeX out of the box was unable to compile these plots because it kept running out of main memory. To increase main memory (or other memory limits) run the following line from the DOS prompt:

initexmf --edit-config-file pdflatex

This will open the configuration file for pdflatex into an editor (probably notepad). Add the following lines to this configuration file:

pool_size=5000000
main_memory=6000000
extra_mem_bot=2000000
font_mem_size=2000000

I needed lots of memory so these numbers are quite bit. I’ve got 8GB of RAM so let’s be generous (My first computer was the MK14 which had 256 bytes of RAM, things have moved on a little).

Save the file (file name is provided) and then remake the format files. To do this use the command line:

initexmf --dump=pdflatex

I happen to use pdflatex all the time so note that I am specifying pdflatex in each of the commands. If you’re using straight latex then substitute pdflatex with latex.

Talking about big tikz/pfdplot figures, don’t forget to use the external command:

\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize % activate!

This forces tikz to reuse previously stored figures, saves regenerating them all the time. See section 32 of the TikZ manual (Version 2.10 !) for more details.

Wednesday, May 8, 2013

Drawing Pathways with Tikz

Originally Posted on  by hsauro

I had the need to draw a negative feedback around a linear pathway. Normally I would use a vector drawing packages to do this, such as Illustrator or Inkscape. This time I thought I would try the tikz package that allows one to embed diagrams inside LaTeX files in script form. The tikz manual is an excellent source of ideas and in one section they show how to draw syntax diagrams (Section 5, page 60). I re-purposed some of this code to draw the following pathway with a negative feedback:



To draw this diagram I used the following code. The code is split into fours sections. The header and three coding sections. The first coding section places and names the four species nodes. Naming nodes is very used later on. The second section draws the reaction arrows and the final section draws the negative feedback loop. The header specifies the type of arrow that will be used, >=latex’ which is the latex arrow (See section 23, page 256 for more choices of arrows), and the distance between nodes. See the comments in the code to understand how the feedback loop was made.

\begin{tikzpicture}[>=latex', node distance=2cm]

\node (Xo) {\Large $X_o$};
\node [right of = Xo] (S1) {\Large $S_1$};
\node [right of = S1] (S2) {\Large $S_2$};
\node [right of = S2] (X1) {\Large $X_1$};

\draw [->,ultra thick,blue] (Xo) -- node[above, black] {$v_1$} (S1);
\draw [->,ultra thick,blue] (S1) -- node[above, black] {$v_2$} (S2);
\draw [->,ultra thick,blue] (S2) -- node[above, black] {$v_3$} (X1);

% Lets draw a line with a blunt end, -|
\draw [-|,ultra thick,blue]
% start in the middle of S2, and move down 2.75 mm 
% ($ ... $) notation is used to add the coordinates
($ (S2) + (0mm,-2.75mm) $)
% Now draw the line down by 3mm
% -- means draw to, + means move by 
-- +(0,-3mm)
% Now move back to the left of S2
% The symbol -| means draw horizontal then vertical. 
% If we used -- instead the line would be drawn 
% diagonally to the reaction edge.
% (S1) is the center of the node. But we want the
% blunt end to end below the S1 line and
% half way to the left. The 10mm is half the node
% distance of 2cm, and 1mm is slightly below  
% the reaction line.
-| ($ (S1) - (10mm,1mm) $);

\end{tikzpicture}

Wednesday, December 5, 2012

LaTeX and Chemistry

Originally Posted on  by hsauro

I recently had the need to draw some chemical equation in a LaTeX document. In the past I’ve used ChemDraw and loaded the image into my document as a png file. Last time I used ChemDraw was on my old computer and I didn’t have it on my new machine. So rather than download a copy and remind myself how to use it, I decided to see what was going on in the LaTeX community with respect to chemistry support. The answer is a lot. What I found was a number of new packages. One particular package, chemfig, written by Christian Tellechea, caught my attention. One needs to read the documentation before using this package but its mode of operation is fairly straightforward. Other related tools can be found at Clemens Niederberger

For those who are curious, chemfig is built on tikz, an excellent general-purpose drawing package. The main command in chemfig is:

   \chemfig{atom1 bond type[angle,coeff,tikz code]atom2}

Atoms can continue to be added to the list. A very simple example is:

\setatomsep{2em}
\chemfig{CH_3-CH_2-COOH}

\setatomsep just set the bond length which I made a little longer compared to the default. Or expressed in a different way using the angle option:

\chemfig{H-C(-[2]H)(-[6]H)-C(-[2]H)(-[6]H)-C(-[7]H)=[1]O}


For my purpose I need to draw a transaminase reaction, for this, I used the following code:

\setatomsep{1.8em}
\schemestart
\chemname{\chemfig{[6]COO^{-}-[6]CH(-[0]NH_3^{+})%
-[6]CH_2-[6]CH_2-[6]COO^{-}}}{glutamate}
\+{0pt,0pt,-48pt}
\chemname{\chemfig{[6]COO^{-}-[6]C(=[0]O)-[6]CH_3}}{pyruvate}
\arrow
\chemname{\chemfig{[6]COO^{-}-[6]C(=[0]O)-[6]CH_2-[6]CH_2%
-[6]COO^{-}}}{$\alpha$-ketoglutarate}
\+{0pt,0pt,-48pt}
\chemname{\chemfig{[6]COO^{-}-[6]CH(-[0]{NH_3^{+}})%
-[6]CH_3}}{alanine}
\schemestop

which yielded:



It could probably be tuned up a bit, for example, the molecules are a bit close to the `addition' symbols. One thing I couldn’t get working very well was coloring atoms and groups of atoms.

If you use MiKTeX on windows all you have to do is include the following line at the start of your document:

\usepackage{chemfig}

and MiTeX will automatically download the package. Further details of the package including the fairly long manual can be found at CTAN.

Monday, May 23, 2011

Drawing Pathways with TikZ: Part 2

Originally Posted on  by hsauro

Timo Maarleveld, as part of his thesis writing, started using TikZ to draw some of his network diagrams. To give you an example, the figure below was generated by the code listed at the end. IT gives some idea of the potential of using Tikz.




\documentclass{article}

\usepackage{tikz}
\usepackage{subfig}
\usetikzlibrary{shapes,arrows,automata}
\begin{document}

\begin{figure}
\tikzstyle{square}=[rectangle,thick,minimum size=0.5cm,draw=blue!80,fill=blue!20]
\tikzstyle{vspecies}=[rectangle, minimum size=0.5cm,draw=blue!80,fill=blue!20]
\tikzstyle{square}=[rectangle,thick,minimum size=0.5cm,draw=red!80,fill=blue!20]
\tikzstyle{fspecies}=[rectangle, minimum size=0.5cm,draw=red!80,fill=red!20]
\subfloat[Two enzyme linear protein network]{
\begin{tikzpicture}[auto, outer sep=3pt, node distance=2cm,>=latex']
\node [fspecies] (S) {S};
\node [vspecies, right of = S] (X) {X};
\node [fspecies, right of = X] (P) {P};
\draw [<->,thick] (S) --  node {$V_1$} (X) ;
\draw [<->,thick] (X) --  node {$V_2$}(P);
\end{tikzpicture}}

\hspace{15mm}

\subfloat[Six enzyme branched protein network]{
\begin{tikzpicture}[auto, outer sep=3pt, node distance=2cm,>=latex']
\node [fspecies] (S) {S};
\node [vspecies, right of = S] (X1) {$X_{1}$};
\node [vspecies, right of = X1] (X2) {$X_{2}$};
\node [vspecies, above right of = X2] (X3) {$X_{3}$};
\node [vspecies, below right of = X2] (X4) {$X_{4}$};
\node [fspecies, right of = X3] (P1) {$P_{1}$};
\node [fspecies, right of = X4] (P2) {$P_{2}$};
\draw [<->,thick] (S) --  node {$V_1$} (X1) ;
\draw [<->,thick] (X1) --  node {$V_2$}(X2);
\draw [<->,thick] (X2) --  node {$V_3$}(X3);
\draw [<->,thick] (X2) --  node {$V_4$}(X4);
\draw [<->,thick] (X3) --  node {$V_5$}(P1) ;
\draw [<->,thick] (X4) --  node {$V_6$}(P2);
\end{tikzpicture}}

\vspace{10mm}

\subfloat[Three enzyme branched protein network ]{
\begin{tikzpicture}[auto, outer sep=3pt, node distance=2cm,>=latex']
\node [fspecies] (S) {S};
\node [vspecies, right of = S] (X) {$X$};
\node [fspecies,above right of = X] (P1) {$P_{1}$};
\node [fspecies,below  right of = X] (P2) {$P_{2}$};
\draw [<->,thick] (S) --  node {$V_1$} (X) ;
\draw [<->,thick] (X) --  node {$V_2$}(P1) ;
\draw [<->,thick] (X) --  node {$V_3$}(P2);
\end{tikzpicture}}        \hspace{20mm}
\subfloat[Three enzyme linear pathway with moiety conservation]{
\begin{tikzpicture}[auto, outer sep=3pt, node distance=2cm,>=latex']
\node [fspecies] (S) {S};
\node [vspecies] at (0.25,1.2) (CD) {CD};
\draw [<->,thick] (0.25,0.725)  arc (180:360:20pt);
\node [vspecies, right of = S] (SC) {SC};
\node [vspecies] at (1.75,1.2)(D) {D};
\node [vspecies, right of = SC] (PC) {PC};
\node [vspecies] at (4.35,1.2) (D) {D};
\draw [<->,thick] (4.35,0.725)  arc (180:360:20pt);
\node [fspecies, right of = PC] (P) {P};
\node [vspecies] at (5.85,1.2) (CD) {CD};
\draw [<->,thick] (S) --  node {$V_1$} (SC) ;
\draw [<->,thick] (SC) --  node {$V_2$}(PC);
\draw [<->,thick] (PC) --  node {$V_3$}(P);
\end{tikzpicture}}

\vspace{10mm}

\hspace{25mm}
\subfloat[Four enzyme linear pathway with negative feedback]{
\begin{tikzpicture}[auto, outer sep=3pt, node distance=2cm,>=latex']
\node [fspecies] (S) {S};
\node [vspecies, right of = S] (X1) {$X_{1}$};
\node [vspecies, right of = X1] (X2) {$X_{2}$};
\node [vspecies, right of = X2] (X3) {$X_{3}$};
\node [fspecies, right of = X3] (P) {P};
\draw [<->,thick] (S) --  node {$V_1$} (X1);
\draw [<->,thick] (X1) --  node {$V_2$} (X2);
\draw [<->,thick] (X2) --  node {$V_3$} (X3);
\draw [<->,thick] (X3) --  node {$V_4$}(P);
\draw [line width = 1pt,dotted,red] (X3) |-  (1,-1);
\draw [line width = 1pt,dotted,red] (1,-1) --  (1,-0.25);
\draw [line width = 1pt,dotted,red] (0.75,-0.25) --  (1.30,-0.25);
\end{tikzpicture}}
\end{figure}

\end{document}

Monday, May 16, 2011

Top 10 LaTeX Packages

May 16, 2011 4:10 pm

1. amsmath, amsfonts

Particularly handy for rendering matrices. amsmath has a set of tailored matrix environments. For example here are three examples that use pmatrix, bmatrix and Bmatrix. ams also provide a host of new symbols, such as $\therefore$, $\varnothing$, and $\thickapprox$ and new alignment environments. See the user document for further details.

$$\begin{pmatrix} 1 & 2 & 3 & 4\\ 5 & 6 & 7 & 8\\ 9 &10 &11 &12\\ 13&14 &15 &16 \end{pmatrix} \begin{bmatrix} 1 & 2 & 3 & 4\\ 5 & 6 & 7 & 8\\ 9 &10 &11 &12\\ 13&14 &15 &16 \end{bmatrix} \begin{Bmatrix} 1 & 2 & 3 & 4\\ 5 & 6 & 7 & 8\\ 9 &10 &11 &12\\ 13&14 &15 &16 \end{Bmatrix} $$

2. TikZ/Pgf

The TeX world has gone through many approaches that permit the inclusion of figures into LaTeX documents. The latest approach and one gaining popularity is TikZ/Pgf. The best way to appreciate this package is to visit the examples site where numerous user contributed examples can be found. TikZ has a steep learning curve but the results are outstanding.

3. pgfplots

Along side TikZ there is the companion package pgfplots which removes much of the effort in drawing data or function plots within LaTeX documents. See the example site for numerous examples of what pgfplots can do, including 3D plots.

4. geometry

Every struggled with getting page dimensions set for your latest work? If so, the geometry package is the answer. The geometry package can be used to control the overall margins, and text area of your document. For example, the following sets up pages for a standard 6 by 9 book format with additional space for the binding edge, taking into account that the pages will be double-sided.

\usepackage[paperwidth=6in,paperheight=9in,top=1in,left=0.66in,right=0.66in,textheight=6.8in,bindingoffset=5mm,twoside]{geometry}

5. xcolor

If you want driver-independent color and access to different kinds of color specifications, then xcolor is the package. xcolor comes with an enormous set of standard colors in addition to being able to mix your own in a wide variety of ways, including rgb, cmyk, hsb, to name but a few.

6. booktabs

The standard tables support in LaTeX isn't very good at spacing the rows. Instead I use booktabs which does a much better job.

\begin{center}
{\bfseries Normal LaTeX}\newline\newline
\begin{tabular}{llr}
\hline \multicolumn{2}{c}{Item} \\ \cline{1-2} Animal & Description & Price (\$) \\ \hline Gnat  & per gram & 13.65 \\ & each     &  0.01 \\ Gnu   & stuffed  & 92.50 \\ Emu   & stuffed  & 33.33 \\ Armadillo & frozen & 8.99 \\ \hline \end{tabular} \end{center} [+preamble] \usepackage{booktabs} [/preamble] \begin{center}  \newline\newline{\bfseries With Booktabs}\newline\newline   \begin{tabular}{llr} \toprule \multicolumn{2}{c}{Item} \\ \cmidrule(r){1-2} Animal & Description & Price (\$) \\ \midrule Gnat  & per gram & 13.65 \\ & each     &  0.01 \\ Gnu   & stuffed  & 92.50 \\ Emu   & stuffed  & 33.33 \\ Armadillo & frozen & 8.99 \\ \bottomrule
\end{tabular}
\end{center}

7. fancyhdr

fancyhdr is useful for customizing the headers and footers in your document. For example, to place today's date on the right hand side of the page header and the title of the document on the left side, one can use:

\lhead{My Thesis}
\rhead{\today}

fancyhdr also comes with an additional pagestyle called fancy, as in \pagestyle{fancy}. fancy will add a line at the top of every page (except pages such as new chapter, title page etc.).

8. quotchap

quotchap redesigns the chapter heading page. In particular it will put a large grey chapter number to the right of the page and can also be used to optionally add chapter quotations

9. hyperref

I tend to always use pdflatex these days to generate my output. In generating pdf files, it is very useful to have hyperlinks automatically inserted into the document. hyperref is the package to use.  These include hyperlinks from the chapter contents page, equation references, figure references and index references. Adding hyperlinks to your pdf output is very simple, just include the line:

\usepackage{hyperref}

near the start of your document. There are many options for changing the style, including color and how the hyperlink is presented.

10. kbordermatrix

Adding column and row labels to matrices is not always easy. kbordermatrix however does the trick. An example is shown below. There is a lot more flexibility to kbordermatrix than shown here so I refer you to the documentation for more details.

\kbordermatrix{\mbox{indices}&1&2&3&4\\
1&M_{1,1}&M_{1,2}&M_{1,3}&M_{1,4}\\
2&M_{2,1}&M_{2,2}&M_{2,3}&M_{2,4}
}

I am sure others will have their own favorites.

Packages I've used on Occasion

1. KOMA-Script and Memoir class

Koma-Script and Memoir class are both replacements for the traditional article/report/book classes in LaTeX with the memoir class being a replacement for report and book. Both come with extensive documentation an require some initial effort to use. A summary of the capabilities of Koma-script can be found at stackexchange.

Details on the memoir class can be found at the PracTeX Journal article. To use these packages effectively requires a thorough reading of the manuals.

2. natbib

natbib is a reimplementation of the \cite command. As a result it is much more flexible in the way references can be cited.  See reference sheet for details.

3. subfigure

subfigure is the package to use if you need to place small figure or tables within a single figure. It supports options to set the position and captioning. The following code will place three figures in a line. If there isn't enough space, the package will automatically place the overflow subfigures on to the next line.

\begin{figure}[htb]

\subfigure[Subfigure 1 caption]{
   \includegraphics{subfigure1.pdf}
   \label{fig:subfig1}
 }

 \subfigure[Subfigure 2 caption]{
   \includegraphics{subfigure2.pdf}
   \label{fig:subfig2}
 }

 \subfigure[Subfigure 3 caption]{
   \includegraphics{subfigure3.pdf}
   \label{fig:subfig3}
 }

\label{myfigure}
\caption{Global figure caption}
\end{figure}


Thursday, May 5, 2011

Graphics and LaTeX

May 1, 2011 4:17 pm

I've been using TeX/LaTex for many years and have observed at close hand how LaTeX has evolved with the addition of new editors, bibliography support and particularly support for graphics. In my book "Enzyme Kinetics for Systems Biology", I use three mechanisms for including illustrations. The first is simply to use \includeGraphics to include an external file. I use pdflatex exclusively now and in this case I include external pdf files into the document. For example the following LaTeX snippet will include the external pdf file called "externalPdfFile". includeGraphics has a lot of flexibility for scaling the image, rotating the image etc.

\begin{figure}[htb]
\begin{center}
\includegraphics[scale = 0.5]{externalPdfFile}
\caption{Include an external pdf file into the document.}
\label{fig:externalPdfFile}
\end{center}
\end{figure}

Pdf illustrations can be either produced by commercial products such as Adobe Illustrator or open source offerings such as Inkscape.

A modern TeX/LaTeX based solution to illustrations and data graphing is PGF/Tikz and PGFPlot. Both these tools are extremely useful for creating diagrams and data plots. The example below illustrates a simple plot generated using pdfplot showing the difference between a hyperbolic and sigmoidal response. The advantage, as least I see it as an advantage, to using something like PGFPlot, it that the information to generate the plot can be embedded in the text itself. Now I know many will say it is good practice to separate presentation from content but from my point of view including them in the same file save a lot of trouble. The code itself is given below the graph.




\begin{tikzpicture}
\begin{axis}[
xlabel={Substrate Concentration},
ylabel={Reaction Rate},
xmin=0, xmax=3,
ymin=0, ymax=1,
width=8cm, height=6cm]
\addplot[color=orange,line width=1.5pt] expression[domain=0:3,samples=20]{x^4./(1+x^4.)};
\addplot[color=blue,line width=1.5pt] expression[domain=0:3,samples=20]{x/(0.3+x)};
\end{axis}
\end{tikzpicture}

PGFPlot is based on PGF/Tikz. PGF/Tikz is itself a general-purpose illustration package for LaTeX or TeX. PGF is the low-level language component, while Tikz is a set of higher-level calls to PGF. For most users, the Tikz layer (now at version 2.10) is the most appropriate to use. The Tikz manual (over 700 pages) is one of the best manuals I've come across, containing many examples and tutorials. The small biochemical network below was produced using Tikz. For those who use SBML (Systems Biology Markup Language), there is also the package SBML2Tilkz that can convert SBML models into ready-to-go Tikz files.



The code for the above diagram is given here:

\begin{tikzpicture}
\draw(38pt,50pt) node[anchor=west] {$S_1$};
\draw(102pt,50pt) node[anchor=west] {$S_2$};

\draw[-stealth,color=blue,very thick] (50pt,60pt) to [controls=+(50:1) and +(130:1)] (110pt,60pt);
\draw[stealth-,color=blue,very thick] (50pt,40pt) to [controls=+(130:-1) and +(50:-1)] (110pt,40pt);

\draw[-stealth,color=blue,very thick] (50pt,93.5pt) to [controls=+(130:-1) and +(50:-1)] (110pt,93.5pt);
\draw[stealth-,color=blue,very thick] (50pt,6.5pt) to [controls=+(50:1) and +(130:1)] (110pt,6.5pt);

\draw(72pt,15pt) node[anchor=west] {$v_1$};
\draw(72pt,85pt) node[anchor=west] {$v_2$};

\draw(36pt,100pt) node[anchor=west] {$A$};
\draw(105pt,100pt) node[anchor=west] {$B$};

\draw(36pt,0pt) node[anchor=west] {$D$};
\draw(105pt,0pt) node[anchor=west] {$C$};
\end{tikzpicture}

Note that the WordPress QuickLaTex allows one to embed PGFPlot or Tikz code in the WordPress page itself although it can be a bit slow to render. The above figures were in fact rendered first using QuickLaTeX then png copies were extracted using "right-click" and "Save image as". The images were then inserted into the WordPress page. This significantly improved the rendering speed.


Wednesday, May 4, 2011

Drawing Pathways with TikZ: Part 1

May 4, 2011 9:45 am

Drawing biochemical pathway diagrams has never been easy. Many of us probably use a combination of Power Point, illustrator, Inkscape, or even MS Paint. Of these Illustrator and Inkscape are probably the most useful but Illustrator is expensive and Inkscape is sometimes a little buggy. Power Point is of course a favorite because it's used so often, perhaps too often in our everyday work. In using these tools we are also forced to maintain separate files from our LaTeX document and in formats that are not related to LaTeX itself.

Although there is an initial learning curve, the LaTeX extension, PGF/TikZ is an alternative to drawing pathway diagrams. Timo Maarleveld, who is doing a graduate project at UW, has played around with TikZ and inspired me to take a second look at TikZ. Last year one of my students published an article on converting SBML to TikZ (www.SBML2Tikz.org) but that approach used absolute coordinates to layout the network. In the following example I have a very simple cycle that is drawn using TikZ that uses some relative positioning though the intersection of the curves is still done using an absolute distance.


\begin{tikzpicture}[>=latex',node distance = 2cm]
\node (S1) {$S_1$};
\node [left of = S1] (S2) {$S_2$};
\node [above of = S2,node distance=1.04cm] (A) {$A$};
\node [above of = S1,node distance=1.04cm] (B) {$B$};
\node [below of = S2,node distance=1.04cm] (C) {$C$};
\node [below of = S1,node distance=1.04cm] (D) {$D$};

\draw [->,thick] (S1) to[bend left=40] node {} (S2);
\draw [->,thick] (S2) to[bend left=40] node {} (S1);
\draw [->,thick] (A) to[bend right=40] node {} (B);
\draw [->,thick] (D) to[bend right=40] node {} (C);
\end{tikzpicture}


I wasn't particularly satisfied with this solution mainly because I use absolute dimensions
(The 1.04cm) to make sure that the curves intersected at the midpoint. I therefore
tried tex.stackexchange where I got the following answer from Dmitry F. Volosnykh:

\documentclass{minimal}

\usepackage{tikz}
\usetikzlibrary{arrows,positioning}

\def \coeffx{0.29}
\def \coeffy{0.91}
\def \nodedistancex{4cm}
\def \nodedistancey{1cm}
\def \offsetx{\nodedistancex*\coeffx}
\def \offsety{\nodedistancey*\coeffy}

\begin{document}

\begin{tikzpicture}[>=latex', node distance=\nodedistancey and \nodedistancex]
 \node (S1) {$S_1$};
 \node[left=of S1] (S2) {$S_2$};
 \node[above=of S2] (A) {$A$};
 \node[above=of S1] (B) {$B$};
 \node[below=of S2] (C) {$C$};
 \node[below=of S1] (D) {$D$};

 \draw[->,thick] (S1) .. controls +(-\offsetx,\offsety) and +(\offsetx,\offsety) .. (S2);
 \draw[->,thick] (S2) .. controls +(\offsetx,-\offsety) and +(-\offsetx,-\offsety) .. (S1);
 \draw[->,thick] (A) .. controls +(\offsetx,-\offsety) and +(-\offsetx,-\offsety) .. (B);
 \draw[->,thick] (D) .. controls +(-\offsetx,\offsety) and +(\offsetx,\offsety) .. (C);
\end{tikzpicture}

\end{document}

There was an additional answer from Frédéric which you can see form this link.