Graphics, Figures & TablesAlignment of tabular and coordgrid on a test

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
Singularity
Posts: 156
Joined: Sat Jan 22, 2011 9:55 pm

Alignment of tabular and coordgrid on a test

Post by Singularity »

I'm looking for help aligning and spacing the table and coordinate grid in the following MWE. This is supposed to be a test question. The tabular is the data to be graphed in the coordinate grid. I would like tabular's and coordinate grid's top to be about the same level and have some space between them, which the \qquad is failing to do.

A secondary issue is figuring out how to add the axes from -10 to 10 to the coordinate grid.

Code: Select all

\documentclass[fleqn,addpoints
%,answers
]{exam}
\usepackage{amsfonts,amsmath,amssymb,amsthm,mathtools}
\usepackage{color}					% Necessary for \shaded solutions and color solutions
\usepackage{coordsys}

\usepackage{array}					% Provides for a more flexible array and tabular environment
\newcolumntype{L}{>{\begin{math}}l<{\end{math}}}%
\newcolumntype{C}{>{\begin{math}}c<{\end{math}}}%
\newcolumntype{R}{>{\begin{math}}r<{\end{math}}}%

\everymath{\displaystyle}
\setlength{\parindent}{0pt}
\setlength{\parskip}{\baselineskip}
\shadedsolutions

\newcommand{\sol}[1]{\begin{solution} #1 \end{solution}\vspace{\stretch{1}}}

\begin{document}
\begin{questions}

\question[2] Construct a scatterplot for the given data (label the axes).

\begin{tabular}[b]{R|R}
	x & y \\
	\hline
	-3 & -7 \\
	-2 & -3 \\
	-1 & -2 \\
	-4 & 1 \\
	-2 & 2 \\
	6 & 1 \\
	2 & -5 \\
	9 & 2 \\
	-5 & -4 \\
	-1 & -4
\end{tabular}\qquad\qquad
\coordgrid(0,0)(200,200)

%\begin{oneparchoices}
	%\cc 	\includegraphics{T2_Q4_A.png}
	%\ch 	\includegraphics{T2_Q4_B.png}\newline
	%\ch 	\includegraphics{T2_Q4_C.png}
	%\ch 	\includegraphics{T2_Q4_D.png}
%\end{oneparchoices}
%\al{A}\vspace{\stretch{1}}
\sol{\coordgrid(0,0)(200,200)}

\end{questions}

\end{document}
Note that to add the axes lines, I tried what was in the documentation, as follows, but could not get it to work:

This one prints the system the right size, but does not overlay it, and has the wrong labels:

Code: Select all

\coordgrid(0,0)(200,200)
\coordsys(0,0)(200,200)

This would probably have the right labels, except that it's really small and offset to the left and also does not overlay the grid:

Code: Select all

\coordgrid(0,0)(200,200)
\coordsys(-10,-10)(10,10)

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

Alignment of tabular and coordgrid on a test

Post by Johannes_B »

You can use minipages to space out the stuff on the page.

The documentation promised superimposing, i couldn't get both
grids to be superimposed. I don't know why this behaviour is in
action.

An alternative could be TikZ and especially package pgfplots.

Code: Select all

\documentclass[fleqn,addpoints
	,answers
]{exam}
\usepackage{amsfonts,amsmath,amssymb,amsthm,mathtools}
\usepackage{color}
\usepackage{coordsys}
\usepackage{pgfplots}

\usepackage{showframe}
\usepackage{array}
\newcolumntype{L}{>{\begin{math}}l<{\end{math}}}%
\newcolumntype{C}{>{\begin{math}}c<{\end{math}}}%
\newcolumntype{R}{>{\begin{math}}r<{\end{math}}}%

\everymath{\displaystyle}
\setlength{\parindent}{0pt}
\setlength{\parskip}{\baselineskip}
\shadedsolutions

\newcommand{\sol}[1]{\begin{solution} #1 \end{solution}\vspace{\stretch{1}}}

\begin{document}
\begin{questions}

	\question[2] Construct a scatterplot for the given data (label the axes).

	\begin{minipage}{.3\linewidth}
		\begin{tabular}{R|R}
			x & y \\
			\hline
			-3 & -7 \\
			-2 & -3 \\
			-1 & -2 \\
			-4 & 1 \\
			-2 & 2 \\
			6  & 1 \\
			2  & -5 \\
			9  & 2 \\
			-5 & -4 \\
			-1 & -4
		\end{tabular}
	\end{minipage}\hfill
	\begin{minipage}{.6\linewidth}

		\rescaleby{1}{10}{\vlabel}
		\rescaleby{1}{10}{\hlabel}
		\coordgrid(-50,-100)(80,50)%
		\coordsys(-50,-100)(80,50)
	\end{minipage}

	\begin{solution}

		\begin{tikzpicture}
			\begin{axis}[
					width=.7\linewidth,
					axis lines=middle
				]
				\addplot [only marks,
					mark=x
				] table {
					-3  -7 
					-2  -3 
					-1  -2 
					-4  1 
					-2  2 
					6  1 
					2  -5 
					9  2 
					-5  -4 
					-1  -4
				};
			\end{axis}
		\end{tikzpicture}

	\end{solution}
\end{questions}

\end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
Singularity
Posts: 156
Joined: Sat Jan 22, 2011 9:55 pm

Re: Alignment of tabular and coordgrid on a test

Post by Singularity »

OK, I see that the 'rescaleby' is somehow rescaling the following two commands (I will read up on 'rescaleby'), and the spacing is better. But the axes are not overlaying the grid in WriteLatex. Instead, it displays the grid above (not superimposed on) the axes. So I still need to get that part working.

When I compile it on my PC, I also get some boxes drawn around the top and right margins of the page, and I get a warning that pgfplots is running in backward compatability mode due to incompatible tick marks. Can those boxes be removed?

Can gridlines be superimposed on the tikz picture, instead? If so, I can use that (without the answers). But note that when I tried the tikz picture without the points in the table, the axes didn't have the same scale. I will read about tikz, too.

I will read up about rescaleby, pgfplots and tikz. Thanks for your help.
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Alignment of tabular and coordgrid on a test

Post by Johannes_B »

I wrote myself that i weren't able to correctly superimpose the grids. It might be a bug, i don't know.

The boxes you describe are put there by package showframe. They visualize the text area, the marginpar-column and the header. I like this when testing as i see how the material is set within the text area. As this is some kind of debug tool, i guess you want to remove it for the final version ;-)

pgfplots is a package designed to make plotting data points easy. You can read files with ten thousands of datapoints, no problem. The axis ranges are calulated based on the data. If no data is given, the package gets confused.
With version 3.0 TikZ (used by pgfplots) has some more capabilities concerning plots. I think it is worth to check them out.
I haven't looked into the new features myself yet.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
Singularity
Posts: 156
Joined: Sat Jan 22, 2011 9:55 pm

Alignment of tabular and coordgrid on a test

Post by Singularity »

Johannes_B wrote:I wrote myself that i weren't able to correctly superimpose the grids. It might be a bug, i don't know.

The boxes you describe are put there by package showframe. They visualize the text area, the marginpar-column and the header. I like this when testing as i see how the material is set within the text area. As this is some kind of debug tool, i guess you want to remove it for the final version ;-)

pgfplots is a package designed to make plotting data points easy. You can read files with ten thousands of datapoints, no problem. The axis ranges are calulated based on the data. If no data is given, the package gets confused.
With version 3.0 TikZ (used by pgfplots) has some more capabilities concerning plots. I think it is worth to check them out.
I haven't looked into the new features myself yet.
Thanks. I didn't even notice the 'showframes' in there.
Post Reply