Graphics, Figures & TablesAuto-resize table cell for graphics inside to fit ?

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
gillouche
Posts: 4
Joined: Thu Apr 09, 2015 9:23 am

Auto-resize table cell for graphics inside to fit ?

Post by gillouche »

TL;DR : I am trying to find an easy and elegant solution to have graphics correctly sized inside a table .

------------------------

Hi !

I am trying to build a kind of cheatsheet for the mathematics subject I am learning again and I have some trouble with the \LaTeX part !

Basically, my problem comes from the graphics (picture taken from wolfram alpha representing function like x^2, x^3, sqrt(x), sin(x), etc... which are sometimes too big for my table cell (table of 2 columns, left column for the graph and right column for some informations about the function like domain, range, ...).

The whole project can be found here but I just started with algebra for the moment :

https://github.com/gillouche/Notes/tree ... hs/algebra

As a MWE, I can provide this :

Code: Select all

\documentclass[10pt,onecolumn]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{multicol}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{mathrsfs}
\usepackage{float}
\usepackage[section]{placeins}
\usepackage{graphbox}
\usepackage{pbox}
\usepackage{longtable}
\usepackage[margin=1.5cm]{geometry}

\DeclareMathSizes{10}{10}{10}{10}
\begin{document}
{\setlength{\parindent}{0pt}

\section{Main/Common functions}

\begin{center}
\begin{longtable}{c|c}
\hline
\multicolumn{1}{|c|}{Graph} & \multicolumn{1}{c|}{Information about f(x)} \\
\hline

\includegraphics[align=c]{graph_x_2.png}
&
\pbox{15cm}
{
  $f(x) = x^2 $ \\
  Domain : $= \mathbb{R}$ \\
  Range : $\{y \in \mathbb{R} \colon y \ge 0 \} $ \\
  Parity : even\\
  Root at x = 0
} \\


\hline
\includegraphics[align=c]{graph_x_3.png}
&
\pbox{15cm}
{
  $f(x) = x^3 $ \\
  Domain : $\mathbb{R}$ \\
  Range : $\mathbb{R}$ \\
  Parity : odd \\
  Root at x = 0
} \\


\hline
\includegraphics[align=c]{graph_sqrt_x.png}
&
\pbox{15cm}
{
  $f(x) = \sqrt(x) $\\
  Domain : $x \in \mathbb{R} \colon x \ge 0 $ \\
  Range : $y \in \mathbb{R} \colon y \ge 0 $ \\
  Root at x = 0
} \\


\hline
\includegraphics[align=c]{graph_ln.png}
&
\pbox{15cm}
{
  $f(x) = \ln(x) $\\
  Domain : $x \in \mathbb{R} \colon x > 0 $ \\
  Range : $ \mathbb{R} $ \\
  Root at x = 1
} \\


\hline
\includegraphics[align=c]{graph_e_x.png}
&
\pbox{15cm}
{
  $f(x) = e^x $\\
  Domain : $\mathbb{R}$ \\
  Range : $y \in \mathbb{R} \colon y > 0 $ \\
  Root : none
} \\

\hline
\includegraphics[align=c]{graph_sin.png}
&
\pbox{15cm}
{
  $f(x) = sin(x) $\\
  Domain : $\mathbb{R}$ \\
  Range : $y \in \mathbb{R} \colon -1 \le y \le 1 $ \\
  Root : $x = \pi n, n \in \mathbb{Z}$\\
  Periodicity : in x with period $2\pi$ \\
  Parity : odd
} \\


\hline
\includegraphics[align=c]{graph_cos.png}
&
\pbox{15cm}
{
  $f(x) = cos(x)$\\
  Domain : $\mathbb{R}$ \\
  Range : $y \in \mathbb{R} \colon -1 \le y \le 1 $ \\
  Root : $x = \pi n - \frac{\pi}{2}, n \in \mathbb{Z}$\\
  Periodicity : in x with period $2\pi$ \\
  Parity : even
} \\


\hline
\includegraphics[align=c]{graph_tan.png}
&
\pbox{15cm}
{
  $f(x) = tan(x)$\\
  Domain : $x \in \mathbb{R} : \pi(n + \frac{1}{2}) < x < \pi(n + \frac{3}{2})$ \\
  and $n \in \mathbb{Z}$ \\
  Range : $\mathbb{R}$ \\
  Root : $x = \pi n, n \in \mathbb{Z}$\\
  Periodicity : in x with period $\pi$ \\
  Parity : even
} \\


\hline
\includegraphics[align=c]{graph_1_over_x.png}
&
\pbox{15cm}
{
$\frac{1}{x}$\\
  Domain : $x \in \mathbb{R} : x \ne 0$ \\
  Range : $y \in \mathbb{R} : y \ne 0$ \\
  Root : none\\
  Parity : odd
} \\


\hline
\includegraphics[align=c]{graph_abs_x.png}
&
\pbox{15cm}
{
$\left|x\right|$\\
  Domain : $\mathbb{R}$ \\
  Range : $y \in \mathbb{R} : y \ge 0$ \\
  Root at x = 0
} \\

\hline
\end{longtable}
\end{center}
\end{document}
Unfortunately, you will have to download the images from the github repository or have a look at the following screenshots (those images are not the result of the code above but all my graphics inside table behave the same way) :

Image
Image

Fyi, I am new to this (I really just started 2 days ago to build things with \LaTeX so I probably made obvious mistakes.

I am open to any suggestions, I never created image of function until now and taking pictures from wolfram alpha seemed like a good idea but that causes a lot of problems. If it is easier to create the image of functions I need with \LaTeX... I would be happy to hear about it.

Thanks a lot !

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Auto-resize table cell for graphics inside to fit ?

Post by Johannes_B »

Hi and welcome,

you can give more optional arguments to the includegraphics commnd, for example width, height and even scale.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
gillouche
Posts: 4
Joined: Thu Apr 09, 2015 9:23 am

Auto-resize table cell for graphics inside to fit ?

Post by gillouche »

Hi,

thanks !

I already tried the scale option for example scale=0.5 but my cell is scaled down too, not only my image. It is like my cell doesn't detect the "real" size of the image I am trying to position in it.

Maybe there is a problem because the sides of my image are white and \LaTeX doesn't detect them properly ? I'll try to create a border in black and see if that changes anything.

Is there a way to automatically do the following :

The image should be centered in the left cell.
If the image is too big, the cell/row is resized (image will still be centered).
If the text on the right cell is too big, the row is resized for the text ?
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Re: Auto-resize table cell for graphics inside to fit ?

Post by Johannes_B »

Personally, i wouldn't even use tabulars here. Not only does it look ugly, it is a bit of a misuse.
A made a further commit, no pull request though, that defines a new environment in which the figure name can be specified. An optional argument is available to set another size if needed.
This is quite hardcoded, and often one finds adding features to the environment, doing stuff, hacking there and much later realises, that it would have been better not to make a macro for that. So here, macros are a blessing and a curse.

You can also plot the functions within LateX directly. It is quite easy, with packages like pgfplots. Please see my very last commit.

I checked your initial problem, and i can see the lines. Depending on the zoom level, a pdf reader cannot manage to display every pixel due to rounding errors. Please test by zooming in and/or out a bit.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
gillouche
Posts: 4
Joined: Thu Apr 09, 2015 9:23 am

Auto-resize table cell for graphics inside to fit ?

Post by gillouche »

Okay, I will remove the tabular. I would like the image and the text close to each other, preferably next to each other.

I think I used table because when the image can be placed anywhere by \LaTeX, I had something like :

text 1
text 2
text 3
image 5
text 4
text 5

image 1
image 4
image 3
image 2

and I couldn't find how to force them to be positioned together.
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Re: Auto-resize table cell for graphics inside to fit ?

Post by Johannes_B »

This is called floating, and in general a good idea. In your case though, floating is unwanted. LaTeX really doesn't care, what you tell it to do, as long as it is sensible. If you tell LaTeX to let something float by putting it inside a floating environment (like figure or table), it will float. Simply omit it.

The commit i was talking about uses minipages to place stuff. The rep is at https://github.com/johannesbottcher/Notes
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
gillouche
Posts: 4
Joined: Thu Apr 09, 2015 9:23 am

Re: Auto-resize table cell for graphics inside to fit ?

Post by gillouche »

Thanks a lot for all your help, it looks much better and feel cleaner now. :)

I am going to try to change your new env to use tikzpicture instead of the graphics.
Post Reply