3

I am trying to draw an EKG tracing using TikZ. This is an example of a single heartbeat on an EKG. The following code generates an acceptable simulacrum, but it's hard to use this code as a generator.

 \documentclass{article}
 \usepackage{tikz}
 \usetikzlibrary{calc}


\newcommand{\PR}[1]{\draw (0,0) -- (#1,0);}
\newcommand{\QRS}[2]
{
    \draw (0,0) -- (#2/2,#1) -- (#2,0);
}
\newcommand{\ST}[1]
{
    \draw (0,0) -- (#1/3,0);
    \draw (#1/3,0) arc (180:0:#1/3);
    \draw (#1,0) -- (#1*1.5,0);
}    
\begin{document}

\begin{tikzpicture}
 \PR{2}
 \begin{scope}[shift={(2,0)}]
    \QRS{3}{2}
  \end{scope}
  \begin{scope}[shift={(4,0)}]
     \ST{2}
   \end{scope}
\end{tikzpicture}

\end{document}

I tried to stick the code in a function:

\newcommand{\RR}[1]
{
    %Healthy PR segment duration upper bound = 0.20s
    %Healthy QRS segment duration upper bound = 0.12s
    %Healthy ST segment duration upper bount = 0.12s

    %Fix QRS amplitude for now
    %This inspires the ratio, 2/1/1

    \PR{#1/2}
    \begin{scope}[shift={(#1,0)}]
        \QRS{3}{#1/4}
        \begin{scope}[shift={(#1/2,0)}]    
            \ST{#1/4}
            \begin{scope}[shift=(#1/2,0)]
                \PR{#1/2}
            \end{scope}
        \end{scope}
    \end{scope}
 %Yes I know this sums to more than 1.
}

This function times out, presumably because of all the nested scopes. How can I draw an EKG more elegantly?

  • scopes are not loops. I assume that you meant to have a loop, right? – A.Ellett May 19 '15 at 19:14
  • I don't use any loops. I was compiling on ShareLaTeX and it timed out. However, for a series of, say, 10 beats, surely there has to be a better way than 180 scope statements. – mac389 May 19 '15 at 19:53
  • 1
    This is a very similar to this one... – Thruston May 19 '15 at 20:25
3

Perhaps this will work for you:

\documentclass{article}
\usepackage[margin=0.5in]{geometry}
 \usepackage{tikz}
 \usetikzlibrary{calc}

\newcommand\aelastnode{\coordinate (lastnode);}
\newcommand{\PR}[1]{  \draw (lastnode) -- ++(#1,0) coordinate (lastnode);}
\newcommand{\QRS}[2]{ \draw (lastnode) -- ++(#2/2,#1) -- ++(#2/2,-#1) coordinate (lastnode);}
\newcommand{\ST}[1]{  \draw (lastnode) -- ++(#1/3,0) arc (180:0:#1/3) -- ++(#1/3,0) coordinate (lastnode);}    

\begin{document}

\noindent
\begin{tikzpicture}[x=0.25cm,y=0.25cm]
  \aelastnode
  \foreach \myn in {1,...,10}
  {
    \PR{2}
    \QRS{3}{2}
    \ST{2}
  }
\end{tikzpicture}

\end{document}

Here I've made the heart beats positioned relative to the last one. So each macro redefines lastnode to get the continuation of the EKG.

enter image description here

3

You can define the curve using a pic and then place that repeatedly using a \foreach loop.

Here's an example using the shape from http://commons.wikimedia.org/wiki/File:EKG_Complex_en.svg, converted to TikZ code using SVG2TikZ:

 \documentclass{article}
 \usepackage{tikz}

\begin{document}

\begin{tikzpicture}[line join=round, x=2pt, y=-2pt]
\tikzset{
    normal ecg/.pic={
        \draw (0,455.0021) -- (11.8345,455.0021);
        \draw (11.8345,455.0021) .. controls (14.2834,454.8958) and
          (14.1385,448.7114) .. (18.8842,448.7114) .. controls (24.2116,448.7114) and
          (23.9695,454.8958) .. (26.3035,455.0021);
        \draw (26.3035,455.0021) -- (40.7723,455.0021);
        \draw (40.7723,455.0021) -- (42.7455,463.0645) -- (46.0339,413.3461)
          -- (48.6647,466.4235) -- (51.2955,455.0021);
        \draw (51.2955,455.0021) -- (61.1605,455.0021);
        \draw (61.1605,455.0021) .. controls (64.4487,454.3298) and
          (65.7118,441.6860) .. (70.3679,441.5241) .. controls (75.2428,441.3542) and
          (76.9447,454.3301) .. (80.2329,455.0021);
        \draw  (80.2329,455.0021) .. controls (81.4852,455.0021) and
          (82.2677,452.8462) .. (84.1792,452.9863) .. controls (85.8717,453.1101) and
          (86.4830,455.0021) .. (87.4678,455.0021);
        \draw (87.4678,455.0021) -- (100,455.0021);
    }
}
\foreach \x in {0,...,3}{
\pic [very thick, scale=0.5] at (\x*50, 0) {normal ecg};
}
\end{tikzpicture}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.