How to insert LaTeX illustrations: image calling method
Introduce the standard method of inserting pictures in LaTeX: load the graphicx macro package, wrap includegraphics with the figure environment, complete numbering and cross-referencing with the figure title and label, and explain the path, format, and troubleshooting points when compilation fails.
Key points of this article
- The picture should be put into the figure environment, with caption and label, so that it can be automatically numbered and quoted in the text.
- The image path must be written relative to the main tex file, and the subfolder must be written in the path. Do not use the absolute path of the computer.
- When using pdfLaTeX, PDF, PNG, and JPG can be imported directly. It is recommended to export SVG to PDF before use.
- The width is scaled using the textwidth ratio parameter. The image does not exceed the center of the plot and is still suitable when changing the paper size.
- The label should follow the caption immediately. After compiling twice, check whether the figure number is consistent with the text reference.
The introductory paragraph needs to directly state the writer's confusion. When many people write a LaTeX paper for the first time, the formulas and symbols can be found, but the pictures cannot be inserted: the compilation error is "File not found", the picture position has moved to the next page, or the figure number and the text reference do not match. Illustration and formula input are two different things, and the main ones used are the graphics environment and file path.
This article is written for people who already know how to write basic LaTeX documents and want to put experimental diagrams, schematics or screenshots into papers or reports. After reading, you should be able to determine where the picture should be placed, what command to use to import it, how to add a picture title and cross-reference, and know where to check first when compilation fails.
The following commands are based on the graphicx macro package, which is a commonly used macro package for LaTeX illustrations.
How to illustrate in LaTeX? One sentence conclusion
It can also be displayed by writing picture commands directly in the text, but it does not have a title or number, and does not participate in floating layout. For formal documents that require numbering, citations, and figure titles, it is recommended to use the figure environment to wrap \includegraphics, and then use \caption and \label.
| your situation | Recommended practices | reason |
|---|---|---|
| A formal accompanying figure in a paper or report | figure environment + \includegraphics + \caption + \label | Automatic numbering, \ref cross-reference available, typesetting position adjusted by LaTeX |
| Several pictures need to be displayed side by side | subfigure or subfigure-like environment using the subcaption macro package | Each sub-picture can be individually marked with (a) and (b) and share a general picture title. |
| Only temporarily place a small icon or formula diagram in the text | Use \includegraphics directly in a line or paragraph | No need for numbering, and no wish to float elsewhere |
| Image is SVG or other unusual format | Convert to PDF or PNG first, then import | The commonly used pdfLaTeX does not directly support SVG, and it is easier to convert after conversion. |
| The image is very large and exceeds the width of the plot. | Scaling with width parameters like width=0.8\textwidth | Scale according to text width proportion, still suitable when changing paper size |
Before you start: Confirm these points first
Before starting, confirm the following items to avoid most compilation errors.
- The image format and compilation engine must correspond. When compiling with pdfLaTeX, PDF, PNG, and JPG can be imported directly; when using XeLaTeX or LuaLaTeX, these formats are also supported. The old EPS format was only used in the old latex plus dvips process and is less common now. If you have SVG in hand, it is more stable to export it to PDF first.
- The image path should be relative to the main file.
\includegraphicssearches for images from the directory where the main.texfile is located by default. If the picture is placed in thefiguressubfolder, write it asfigures/result.pdf. Do not write the absolute path on the computer, otherwise it will become invalid if you change the machine or package the project and send it to others. - File names should use English, numbers and dashes as much as possible. Some environments are unstable in handling Chinese paths or spaces. When there are spaces in the file name, the entire path must be enclosed in curly braces and kept consistent.
- Plan the width of the graph in advance. The layout width of the paper is usually limited. A large picture exported from a drawing software can be placed directly into the paper and may exceed the margin of the page. First think about how wide the picture will occupy in the text, and then decide on the parameters.
- Confirm whether the image is final version. Once the image is replaced, LaTeX will not automatically update the compiled PDF and needs to be recompiled; if the image is modified, it is recommended to retain the original file name or update the reference path simultaneously to avoid mixing old and new versions.
Steps to insert a picture with a title into the document
- Put the picture files into the project directory. It is recommended to create a
figuressubfolder to manage all pictures uniformly. - Confirm that
\usepackage{graphicx}has been written in the preamble area, which provides the\includegraphicscommand. - Write
\begin{figure}[htbp]in the text where the picture needs to be placed. The parameters in square brackets indicate the floating position preference.his here,tis the top of the page,bis the bottom of the page, andpis a separate page. - Write
\centeringin thefigureenvironment to center the image. - Write
\includegraphics[width=0.8\textwidth]{figures/result.pdf}, where the width parameter indicates that the image width is the width of the text multiplied by 0.8. The curly brackets are the relative path and file name, and the extension can be omitted. - Write
\caption{实验结果随训练轮数的变化}, the picture title will be automatically numbered, and it is a common format to put it under the picture. - Write
\label{fig:result}after the figure title, and use如图~\ref{fig:result}所示where you need to quote it in the text to get the figure number. Note that\labelmust immediately follow\caption, otherwise the number may correspond to the wrong number. - Write
\end{figure}to end the environment, and then compile twice to ensure that the cross-reference numbers are stable.
Check each item after completion
After the compilation is completed, don't just check whether there are any errors. It is recommended to check the actual effect of the PDF item by item.
| Check items | How to see | What to do if there is a problem |
|---|---|---|
| Whether the picture is displayed | There is a picture in the corresponding position in PDF, and there is no blank box. | When File not found appears in the log, check that the spelling of the path and extension is consistent with the file |
| Is the picture in the right position? | The picture did not run to the page far away from the text. | Change the parameter from h to htbp, or adjust the paragraph appropriately before the diagram, and use \clearpage to control paging if necessary |
| Size of the graph | The picture does not exceed the margin and the text is clear. | Adjust width parameters, such as 0.6\textwidth or \linewidth |
| Drawing number and reference | References such as "figure 1" in the text are consistent with the title number. | Check whether \label is written after \caption, compile it twice and then read it. |
| Content of picture title | The picture title can explain the content of the picture independently. | Supplement the meaning of coordinates, units or legends, do not just write "result diagram" |
| Picture definition | Zoom in on PDF to check lines and text | Export in vector format, or improve bitmap resolution to avoid looking normal only on the screen |
If your picture is generated by other software, it is recommended to confirm the font size and line weight in the drawing software, zoom and then check the PDF. The text in the picture is too small to be seen clearly after zooming.
# which method is suitable for you?
- write a single paper with fewer pictures: use figure environment plus \caption and \label, the structure is clear, and it is easy to change the drawing number in the later stage. - compare multiple images: use subcaption macro package to combine sub-images and mark each sub-image to make it more intuitive to read. - accustomed to graphical interface, not familiar with commands: you can first write the structure with a desktop or web editor, and then decide whether to export to a LaTeX source file. The UniDoc Page Editor (app.unidoc.top) supports round-trip editing between Word and LaTeX, which is suitable for switching between the two formats, but the specific effect of image insertion is still recommended to be compiled and checked.
# frequently asked questions (FAQ)
1. What if I can't find the picture when I compile?
First check whether the file name in the error report is exactly the same as the file in the directory, including case and extension. Then make sure that the path is written relative to the main .tex file, and that the subfolders are written into the path, such as figures/fig1.pdf.
2. The position of the picture has been wrong all the time after the picture is inserted. Can it be forcibly fixed?
You can change the floating parameter to [H], which needs to be supported by the float macro package, and will display the diagram strictly according to the code location, but it is easy to leave a large blank. It is generally recommended to keep [htbp], adjust before and after paragraphs if necessary, or use \clearpage to control paging.
3. Can the picture be displayed as an illustration without a number?
Sure. Directly use \includegraphics to put in the paragraph, without the figure environment, there will be no number and title. If you just put a small icon, it's easier.
4. Why is the picture blurred when enlarged?
Mostly the original image is a low-resolution bitmap. It is recommended that you export in a vector format such as PDF or EPS, or switch to a higher resolution PNG. Screenshots usually do not have enough resolution and need to be re-exported.
5. Can I use the same command for the inserted picture and the formula?
No. The formula is entered through the mathematical environment, and the picture is introduced through \includegraphics, and the numbering, typesetting and citation of the two are also different. When mixed together, it is recommended that the formula still use the mathematical environment, and the pictures are put into the figure environment.
# Summary
In the illustration in LaTeX, the core is to load the graphicx macro package, wrap the \includegraphics in the figure environment, and complete the numbering and reference with \caption and \label. The path is written relative to the main file, the format corresponds to the compilation engine, and PDF is checked item by item after compilation, which can basically solve most of the illustration problems.
Open OmniDoc to edit, convert or translate your LaTeX document


