3

I'm a new user of LyX, and I've no much experience with LyX in general, and LaTeX packages especially.

I found that it's possible to plots graphs and shapes in 2D and 3D with PGFPlots.

I found some PDF that shows how to plot functions, but not geometrical shapes. I was able to plots a function, but I'm trying to draw rectangles and I don't know how.

I'm working in LyX, and this is my Premables (edit: didn't post that initially because I assumed that most of it wasn't relevant):

\renewcommand{\labelenumi}{\alph{enumi}.}
\renewcommand{\theenumii}{(\roman{enumii})}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\usepackage{dtk-logos}
\date{}
\usepackage[T1]{fontenc}
\usepackage{fancyhdr}
\fancyhf{}

This is my LaTeX code for the graph of the function:

\begin{tikzpicture}
\begin{axis}
    \addplot [blue, line width = 1, smooth, domain=0:9] {sqrt(x)};
\end{axis}
\end{tikzpicture}

What I wanted to add is rectangle such that:

  • x_i is the start x value
  • x_f is the end x value
  • h is the height of the rectangle

I'm describing them in this system, and I don't know if it actually works like that in PGFPlots.

The rectangles are:

  1. (just a line - I suppose that I can draw a function for this)

    • x_i = 0
    • x_f = 1
    • h = 0
    • x_i = 1
    • x_f = 4
    • h = 1
    • x_i = 4
    • x_f = 9
    • h = 2

I want in the graph to have a function, the rectangles, and also a name for the graph.

I'm sorry if I'm asking too much - I've no idea how to do this. And if this question is against the rules, please tell me what rule(s) it's against.

Thanks a lot in advance.

8
  • Could you complete your code so it can be compiled?
    – cfr
    Commented Mar 16, 2017 at 17:03
  • What do you mean by that? Complete it in what way? The code right now compiles correctly. What I'm looking for is what code to write that would generate such rectangles. If I add more code that has a correct syntax, it would compile. Commented Mar 16, 2017 at 17:06
  • First you are talking about rectangles, then triangles and finally rectangles again. Drawing rectangles is quite an easy task (see e.g. tex.stackexchange.com/q/29786), but you just stated the height of the rectangle, but not at which height the rectangles should start ... Commented Mar 16, 2017 at 17:09
  • I meant "rectangle". Thanks for that, and for the link. I didn't find something like that when I searched. Commented Mar 16, 2017 at 17:10
  • 1
    A complete example starts with \documentclass and ends \end{document}. It should include packages required to compile the document without error. (Unless you are asking for help with an error, in which case the code should produce the error you want help with.)
    – cfr
    Commented Mar 16, 2017 at 17:30

1 Answer 1

6

Drawing rectangles is very simple.

The one with h = 0 is only a line.

Here is how a MWE should be:

\documentclass{article} 
\usepackage{pgfplots} 
\pgfplotsset{compat=newest} 

\begin{document} 

\begin{tikzpicture} 
\begin{axis} 
\addplot [blue, line width = 1, smooth, domain=0:9] {sqrt(x)};
\draw (0,0) -- (1,0);
\draw (1,0) rectangle (4,1);
\draw (4,0) rectangle (9,2);
\end{axis} 
\end{tikzpicture}

\end{document}

enter image description here

I think ShareLaTeX tutorial could be useful for you.

Edit n. 1

It's not very sensible with such a simple shape, but just to show you the feature... you can create a pic with two parameters: base and height:

\documentclass{article} 
\usepackage{pgfplots} 
\pgfplotsset{compat=newest}

\tikzset{%
    pics/myrec/.style n args={2}{code={%  
            \draw (0,0) rectangle (#1,#2);
    }},
}
\begin{document} 

\begin{tikzpicture} 
\begin{axis} 
\addplot [blue, line width = 1, smooth, domain=0:9] {sqrt(x)};
% for example you can create a pic with two parameter: base and height
\pic at (0,0) {myrec={1}{0}};
\pic at (1,0) {myrec={3}{1}};
\pic at (4,0) {myrec={5}{2}};
\end{axis} 
\end{tikzpicture}

\end{document}

Edit n. 2

If you want to add other options to the pic:

  • if they are equals for all the pics you can add them to the definition (see the thickness in the following example)
  • if they are different for every pic you can put it like \pic [...] (see dashed and dotted) or create another parameter (see the colors).

You can also set a default for the parameters.

\documentclass{article} 
\usepackage{pgfplots} 
\pgfplotsset{compat=newest} 
\tikzset{% 
pics/myrec/.style n args={3}{code={% 
\draw[very thick, #3] (0,0) rectangle (#1,#2); 
}}, 
pics/myrec/.default={1}{0}{pink},
} 
\begin{document} 
\begin{tikzpicture} 
\begin{axis} 
\addplot [blue, line width = 1, smooth, domain=0:9] {sqrt(x)}; 
\pic at (0,0) {myrec};
\pic[dashed] at (1,0) {myrec={3}{1}{green}}; 
\pic[dotted] at (4,0) {myrec={5}{2}{red}}; 
\end{axis} 
\end{tikzpicture} 
\end{document}

enter image description here

5
  • What kind of parameters were you referring to - giving for a rectangle a domain, and parameter of height, for example? Commented Mar 16, 2017 at 18:58
  • Great, thanks a lot! And how do I add parameters to \pic? Just add [my parameters list] before {myrec={foo1}{foo2}? Commented Mar 16, 2017 at 22:44
  • I tried finding online the list of parameters that pic can get but didn't find it. I also tried adding a background. Is it possible to add a background to a pic, and if so, how? Commented Mar 17, 2017 at 6:03
  • @GalGrünfeld In general you can give to the pic the same options you use for any other node. In your example, for instance, if you write \draw[very thick, #3, fill=yellow] (0,0) rectangle (#1,#2); you'll have yellow rectangles.
    – CarLaTeX
    Commented Mar 17, 2017 at 6:49
  • Great, thanks a lot for all the information. :) Commented Mar 17, 2017 at 11:38

You must log in to answer this question.

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