Graph & Picture in LaTeX: Sin(x) Example

  • LaTeX
  • Thread starter student1938
  • Start date
  • Tags
    Graph Latex
In summary, In order to include a figure in a LaTeX document, you need to use the \figure directive and specify the filename of the figure you want to include.
  • #1
student1938
91
1
How do I plot graphs in LaTeX? Example sin(x) to begin with. :frown:

Also, how do I insert pictures in LaTeX? Example, simple circuit diagrams.

student :confused:
 
Physics news on Phys.org
  • #2
You cannot actually graph in LaTeX. You can plot something for example in Mathematica, Matlab, or Gnuplot and export it to a JPEG or EPS file. Look up the \'figure' directive in the LaTeX documentation. 'figure' will allow you to include an EPS or JPEG file.
 
  • #3
What is the \figure syntax for including a .JPG file cause I personally haven' t been able to locate it for a while now.

James
 
  • #4
Any suggestions, guys, I really need to know how to import .jpg files urgently.

James
 
  • #5
I don´t know how to include .jpg, I only know how to include .eps. What I usually do when I have to include a figure is printing it to a file (PostScript printer), then convert it to .eps with Ghostview and then include the .eps-file. Not very elegant and a bit time-consuming but it works.
 
  • #6
you should use eps files because jpegs can't be scaled.
 
  • #7
ok but what' s the syntax for including a .eps file?

James
 
  • #8
\documentclass{article}
\usepackage{graphicx}
\begin{document}
\begin{figure}
\centering
\includegraphics{filename.eps}
\caption{Caption goes here}
\end{figure}
end{document}

I checked to make sure that I put the pictures (.eps) into the same directory (C:) as the other latex files I have been compiling...the caption appears but there is no picture. The other files (no picture) compile just fine.

IT SAYS LATEX ERROR: UNKNOWN GRAPHICS EXTENSION : EPS!

Help, James ! :cry:
 
Last edited:
  • #9
This is the correct LaTeX code for including a figure:

Code:
\documentclass{article}
\usepackage{graphics}
\begin{document}
   \begin{figure}
      \includegraphics{sin}
      \caption{\label{sin}The sine function.}
   \end{figure}
\end{document}

This assumes that your sin(x) graph is in a file called "sin.eps" if you use plain "latex", or in a file called "sin.pdf" if you use "pdflatex".

You could create such a graph by several tools including GNUPlot, GRI, Asymptote, R, Octave, Excel, and GLE. In GLE, you would create a script like this:


Code:
size 12 10
begin graph
   math
   title "f(x) = sin(x)"
   xaxis min -2*pi max 2*pi dticks pi/2 format "pi"
   yaxis dticks 0.25 format "frac"
   let d1 = sin(x)
   d1 line color red
end graph

and save it as "sin.gle". Then you run the GLE program as follows: "gle -d pdf sin.gle" to create "sin.pdf" or "gle -d eps sin.gle" to create the "sin.eps".

Finally, you run LaTeX on your ".tex" file to produce the final result. You can see an example of this using "pdflatex" here:

http://www.gle-graphics.org/latex/graph.pdf [Broken]
 
Last edited by a moderator:
  • #10
Jeans said:
This is the correct LaTeX code for including a figure:

Code:
\documentclass{article}
\usepackage{graphics}
\begin{document}
   \begin{figure}
      \includegraphics{sin}
      \caption{\label{sin}The sine function.}
   \end{figure}
\end{document}

Are you sure about the "\usepackage{graphics}"? Maybe there are couple of packages... In all my documents it "\usepackage{graphicx}".
By the way, we can also specify which file exactly you want by entering the full name, like "sin.png" etc. If you have a priority of file types you want to use (lets say, png if available, otherwise eps, otherwise pdf), you can add after the "\usepackages"s part this line "\DeclareGraphicsExtensions{.png,.eps,.pdf}"
 
  • #11
The-herod said:
Are you sure about the "\usepackage{graphics}"? Maybe there are couple of packages... In all my documents it "\usepackage{graphicx}".
By the way, we can also specify which file exactly you want by entering the full name, like "sin.png" etc. If you have a priority of file types you want to use (lets say, png if available, otherwise eps, otherwise pdf), you can add after the "\usepackages"s part this line "\DeclareGraphicsExtensions{.png,.eps,.pdf}"

Both the graphicx and graphics packages are available. I have used both, but if I understand correctly, I believe graphicx can do more than the graphics package.

student1938 said:
How do I plot graphs in LaTeX? Example sin(x) to begin with. :frown:

Also, how do I insert pictures in LaTeX? Example, simple circuit diagrams.

student :confused:

You can use pstricks to include a plot of a sine curve (for example); it is "in LaTeX" in the sense that all you have to do is type in several lines in your TeX document. I have used it for function plots and data plots, among other things.

However, if the issue is just to include a function plot in a document and you don't already know pstricks, I would agree with the advice in the above posts to use a separate plotting program (like gnuplot) to produce an eps figure, and then include the figure using the graphicx or graphics packages.
 

1. What is the code for creating a graph of the sine function in LaTeX?

The code for creating a graph of the sine function in LaTeX is:

\begin{tikzpicture}\begin{axis}[ xlabel=$x$, ylabel=$\sin(x)$, xmin=-2*pi, xmax=2*pi, ymin=-1, ymax=1, domain=-2*pi:2*pi, samples=100]\addplot[blue, thick]{sin(deg(x))};\end{axis}\end{tikzpicture}

2. How can I change the color of the graph in LaTeX?

To change the color of the graph in LaTeX, you can use the "color" option in the \addplot command. For example, to change the graph's color to red, you can use \addplot[red, thick]{sin(deg(x))}.

3. Can I add a title and axis labels to the graph in LaTeX?

Yes, you can add a title and axis labels to the graph by using the "title", "xlabel", and "ylabel" options in the \begin{axis} command. For example, \begin{axis}[title=Graph of Sin(x), xlabel=$x$, ylabel=$\sin(x)$].

4. How can I change the domain and range of the graph in LaTeX?

To change the domain and range of the graph, you can use the "xmin", "xmax", "ymin", and "ymax" options in the \begin{axis} command. These options allow you to specify the minimum and maximum values for the x and y axes, respectively.

5. Is it possible to add multiple plots to the same graph in LaTeX?

Yes, you can add multiple plots to the same graph in LaTeX by using the \addplot command multiple times. You can also customize each plot by specifying different options, such as color and style, for each \addplot command.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
883
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
2
Replies
36
Views
837
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
959
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
997
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
869
Back
Top