LaTeX Graph & Picture in LaTeX: Sin(x) Example

  • Thread starter Thread starter student1938
  • Start date Start date
  • Tags Tags
    Graph Latex
AI Thread Summary
To plot graphs like sin(x) in LaTeX, it's recommended to use external tools such as Gnuplot or MATLAB to create the graph and export it as an EPS or PDF file. The LaTeX code for including a figure requires the graphicx package, with the syntax being \includegraphics{filename.eps}. Users can also specify file types by using \DeclareGraphicsExtensions to prioritize formats like PNG, EPS, or PDF. For those looking to plot directly within LaTeX, packages like pstricks can be used, but external tools are generally preferred for ease and quality. Proper file placement and format are crucial to avoid compilation errors.
student1938
Messages
91
Reaction score
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
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.
 
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
 
Any suggestions, guys, I really need to know how to import .jpg files urgently.

James
 
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.
 
you should use eps files because jpegs can't be scaled.
 
ok but what' s the syntax for including a .eps file?

James
 
\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:
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
 
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.
 

Similar threads

Replies
4
Views
3K
Replies
8
Views
3K
Replies
4
Views
3K
Replies
6
Views
5K
Back
Top