Graph & Picture in LaTeX: Sin(x) Example

  • Context: LaTeX 
  • Thread starter Thread starter student1938
  • Start date Start date
  • Tags Tags
    Graph Latex
Click For Summary

Discussion Overview

The discussion centers on how to plot graphs, specifically the sine function, and how to insert images, such as circuit diagrams, in LaTeX documents. Participants explore various methods and tools for achieving these tasks, including package recommendations and file formats.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant asks how to plot graphs in LaTeX and insert pictures, specifically mentioning the sine function and circuit diagrams.
  • Another participant states that LaTeX cannot graph directly and suggests using external tools like Mathematica, Matlab, or Gnuplot to create plots and export them as JPEG or EPS files.
  • A participant requests the syntax for including a .JPG file in LaTeX.
  • One contributor mentions their method of converting figures to EPS format using Ghostview, noting it is time-consuming but effective.
  • Another participant advises using EPS files over JPEGs due to scaling issues with JPEGs.
  • Multiple participants provide LaTeX code snippets for including figures, with variations in package usage, such as \usepackage{graphicx} versus \usepackage{graphics}.
  • One participant suggests using the pstricks package for plotting functions directly in LaTeX, while others recommend creating plots with external programs.
  • There is a discussion about the flexibility of specifying file types for graphics inclusion, with suggestions for prioritizing file formats.

Areas of Agreement / Disagreement

Participants express differing views on the best methods for including graphics in LaTeX, with no consensus on whether to use EPS or JPEG files, or which packages are preferable. The discussion remains unresolved regarding the best practices for plotting and including images.

Contextual Notes

Some participants mention limitations in their ability to include images, such as encountering errors related to graphics extensions. There are also varying experiences with different LaTeX packages, which may affect the inclusion of figures.

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 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 11 ·
Replies
11
Views
8K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 6 ·
Replies
6
Views
6K