0

wanted

Can anyone help me as to how I can draw the above picture using TikZ or PSTricks? I am not familiar with those two packages.

3 Answers 3

11

Come on! Jump in to water. Don't just stand on shore if you want to learn swimming.

This is a very easy diagram which you should have tried yourself to get started. Easy? See below:

\documentclass[tikz,border=5]{standalone}
\begin{document}
  \begin{tikzpicture}
    \draw (-1,0) -- (5,0)
          (0,-1) -- (0,3)
          (-1,1) -- (5,1)node[anchor=west,scale=0.5]{$y=1$};
    \draw (0,0) -- (1,1) -- (2,0) -- (3,1) -- (4,0);
    \foreach \x in {1,3}{
    \draw[dashed] (\x,1.1)  -- (\x,0) node[pos=1.1,scale=0.5]{\x/4};
    }
    \path[dashed] (2,1.1)  -- (2,0) node[pos=1.1,scale=0.5]{2/4};
  \end{tikzpicture}
\end{document}

enter image description here

So easy that I may even get a down vote for answering! ;)

For learning/more details on TikZ please refer to its (huge) manual. To get this, run texdoc pgfmanual from the command prompt/terminal or your TeX editor may provide a menu for (La)TeX doc.

2
  • Should I downvote? :)
    – azetina
    Jan 18, 2015 at 6:19
  • @azetina :-) As you wish ;)
    – user11232
    Jan 18, 2015 at 6:20
5

A PSTricks solution:

\documentclass{article}

\usepackage{lmodern,xfrac} % for the fractions
\usepackage{pstricks-add}  % for the rest of the drawing

\begin{document}

\psset{unit = 2, linejoin = 2}
\begin{pspicture}(-0.5,-0.5)(3,1.2)
  \psaxes[labels = none, ticks = none]{->}(0,0)(-0.5,-0.5)(2.5,1.2)
  \multido{\i = 1+1, \r = 0.5+0.5}{3}{%
    \psline[linestyle = dotted](\r,1)(\r,0)
    \uput[270](\r,0){$\sfrac{\i}{4}$}}
  \psline(-0.3,1)(2.5,1)
  \uput[0](2.5,1){$y = 1$}
  \psline(0,0)(0.5,1)(1,0)(1.5,1)(2,0)
\end{pspicture}

\end{document}

output1

Another version, giving the same output but it might be a 'better' one nonetheless, is as follows:

\documentclass{article}

\usepackage{lmodern,xfrac} % for the fractions
\usepackage{pstricks-add}  % for the rest of the drawing

% redefinition of the x-axis labels (printing them in the correct range)
\makeatletter
  \def\pst@@@hlabel#1{%
    \ifnum #1<0
%     <print nothing>
    \else
      \ifnum #1<4
        \sfrac{#1}{4}
      \else
%       <print nothing>
      \fi
    \fi%
}
\makeatother

\begin{document}

\psset{unit = 2, linejoin = 2}
\begin{pspicture}(-0.5,-0.5)(3,1.2)
  \psaxes[
    dx = 0.5,
    labels = x,
    xlabelsep = 0pt,
    ticks = none
  ]{->}(0,0)(-0.5,-0.5)(2.5,1.2)
  \multido{\i = 1+1, \r = 0.5+0.5}{3}{%
    \psline[linestyle = dotted](\r,1)(\r,0)}
  \psline(-0.3,1)(2.5,1)
  \uput[0](2.5,1){$y = 1$}
  \psline(0,0)(0.5,1)(1,0)(1.5,1)(2,0)
\end{pspicture}

\end{document}

output2

Update

Here is a 'manual' another version where the fractions have been reduced:

\documentclass{article}

\usepackage{lmodern,xfrac} % for the fractions
\usepackage{pstricks-add}  % for the rest of the drawing

\def\Label(#1)#2#3{%
  \psline[linestyle = dotted](#1,1)(#1,0)
  \uput[270](#1,0){$\sfrac{#2}{#3}$}}

\begin{document}

\psset{unit = 2, linejoin = 2}
\begin{pspicture}(-0.5,-0.5)(3,1.2)
  \psaxes[labels = none, ticks = none]{->}(0,0)(-0.5,-0.5)(2.5,1.2)
  \Label(0.5){1}{4}
  \Label(1){1}{2}
  \Label(1.5){3}{4}
  \psline(-0.3,1)(2.5,1)
  \uput[0](2.5,1){$y = 1$}
  \psline(0,0)(0.5,1)(1,0)(1.5,1)(2,0)
\end{pspicture}

\end{document}

output3

2

With the mfpic package:

\documentclass[12pt]{article}
\usepackage{mathtools}
\usepackage[metapost]{mfpic}
    \setlength{\mfpicunit}{1cm}
    \opengraphsfile{\jobname}
\begin{document}
\begin{mfpic}[6][3]{-0.25}{1.25}{-0.25}{1.25}
    \lines{(-0.2, 1), (\xmax, 1)}
    \lines{(0, 0), (0.25, 1), (0.5, 0), (0.75, 1), (1, 0)}
    \mfpfor{k = 1, 2, 3} \dotted\lines{(k/4, 0), (k/4, 1)} \endmfpfor
    \tlpointsep{3bp}
    \tlabels{[tc](0.25, 0){$\dfrac{1}{4}$} [tc](0.5, 0){$\dfrac{1}{2}$}
        [tc](0.75, 0){$\dfrac{3}{4}$} [cl](\xmax, 1){$y=1$}}
    \doaxes{xy}
\end{mfpic}
\closegraphsfile
\end{document}

enter image description here

If this file is called, say, graph.tex, compile it with LaTeX to generate a graph.mp MetaPost file, then compile this file with MetaPost to produce the actual (PostScript) graph, and finally graph.tex with (pdf)LaTeX to include this file in your document. It's best automatized with a script (which would depend on your installation and TeX editor of course). It works with LaTeX and pdfLaTeX, and probably with XeLaTeX and LuaLaTeX as well.

2
  • It is a good thing that you are giving this answer so that people get a taste of alternate methods. It would be nice (for beginners) if you give details of how to compile this. Keep pouring answers like this. +1 :)
    – user11232
    Jan 18, 2015 at 0:02
  • @Harish Kumar: I've completed my answer. :-) Jan 18, 2015 at 6:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .