Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How to generate f(x)=x curve in Tikz from 0 to 10 with small straight lines at (1,0), (2,0) etc at x axis and (0,1), (0,2) etc at y axis?

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

The easiest solution is to use pgfplots package which is built on top of PGF/TikZ.

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot[const plot,
            domain=0:10,
            mark=none,
            samples=11] {x};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

See the manual for additional options such as titles, legend,marks etc.

You can also stay in the TikZ notation but your ammunition will be limited.

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[domain=0:10,samples=11]
\draw[very thin,color=gray] (-0.1,-1.1) grid (10,10);
\draw[->] (-0.2,0) -- (10.2,0) node[right] {$x$};
\draw[->] (0,-1.2) -- (0,10.2) node[above] {$f(x)$};
\draw[ultra thick] plot [const plot] (\x,\x);
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer
add comment

If you wish to only use tikz and not pgfplots, here are two methods to get ticks along the axes, which is what I am assuming you are trying to get. I hope the code is self-explanatory.

The code is

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}

\begin{document}

%method 1 : with a loop
\begin{tikzpicture}

\draw[->] (0,0) -- (10.2,0) node[right] {$x$};
\foreach \k in {1,...,10}{\draw (\k,-0.05) -- ++(0,0.1);}

\draw[->] (0,0) -- (0,10.2) node[left] {$y$};
\foreach \k in {1,...,10}{\draw (-0.05,\k) -- ++(0.1,0);}

\draw[blue,domain=0:10] plot (\x,\x);
\end{tikzpicture}

%method 2 : with a decoration
\begin{tikzpicture}

\draw[->] (0,0) -- (10.2,0) node[right] {$x$};
\draw[decoration={ticks,segment length=1cm,amplitude=2pt},decorate] (0,0) -- (10.2,0);

\draw[->] (0,0) -- (0,10.2) node[left] {$y$};
\draw[decoration={ticks,segment length=1cm,amplitude=2pt},decorate] (0,0) -- (0,10.2);

\draw[blue,domain=0:10] plot (\x,\x);

\end{tikzpicture}

\end{document}

The result is

enter image description here

share|improve this answer
    
Thank you very much. –  user12290 Feb 25 '12 at 16:02
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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