I want to draw an x-y-diagram with axis labels. Unfortunately the ylabel is misplaced. It looks as depending on the actual data. When the other data line in the sample below is used instead of the upper line, it looks better.
How can I move the label to the left or (more desirable) how can I tell pgfplot to do it corectly?
% !TEX TS-program = pdflatex
% !TEX encoding = UTF-8 Unicode
\documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[width=13cm,height=8cm,
xlabel={I in mA}, ylabel={U in mV}]
\addplot[only marks,mark=star] coordinates {
% (1.36, -0.0177) (45.38, 0.0273) (74.19, 0.0413)
(100.88, 0.0533) (134.80, 0.0683) (195.27, 0.1073)
};
\end{axis}
\end{tikzpicture}
\end{document}
pgfplots 1.3 has a new feature which allows to move axis labels tight to tick labels automatically. Since this affects the spacing, it is not enabled be default.
Use
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}
in your preamble to benefit from the improved spacing.
Related
I would like to add images in the top left/right or bottom left/right in a two-column page while ignoring the margins and having the text wrap around the picture. How can I also extend the solution to include half page images while ignoring the margins and with text wrapping.
I tried tikz package, but the text doesn't wrap around even when I use the wrapfig package.
For example, this is the code I used to insert image on the top left side of the page:
\documentclass[twocolumn, 12pt]{book}
\usepackage{lipsum}
\usepackage{tikz}
\begin{document}
\lipsum[1-3]
\begin{tikzpicture} [remember picture, overlay]
\node[anchor=north west,yshift=-1.5pt,xshift=1pt]%
at (current page.north west)
{\includegraphics[width=0.5\paperwidth,height=0.5\paperheight]{example.jpg}};
\end{tikzpicture}
\lipsum[1-3]
\end{document}
This results in the picture at the desired position, but the text is obscured behind the image. Thank you!
Not very pretty, but does the job I think (using geometry package only to get the correct values for top margin):
\documentclass[twocolumn, 12pt]{book}
\usepackage{lipsum}
\usepackage{tikz}
\usepackage{geometry}
\begin{document}
\begin{tikzpicture} [remember picture, overlay]
\node[anchor=north west]%
at (current page.north west)
{\includegraphics[width=0.46\paperwidth,height=0.5\paperheight]{example.png}};
\end{tikzpicture}
\vspace*{\dimexpr(0.5\paperheight-\voffset-1in-\headsep-\headheight)}
\lipsum[1-3]
\lipsum[1-3]
\end{document}
I am trying to use the subfigure method shown at https://www.latex-tutorial.com/tutorials/figures/ to make side by side plots but I can't seem to adjust the size and get them to go side by side...what am i doing wrong?
Below is code I am using
\begin{figure}
\centering
\begin{subfigure}[b!]{0.3\textwidth}
\begin{tikzpicture}
\begin{axis}[
axis y line = middle,
axis x line = middle,
xlabel = $x$,
ylabel = {$f(x) = x^3$},
grid=major,
]
\addplot [
domain=-3:3,
samples=100,
color=red,
]
{x^3};
\addlegendentry{$x^3$}
%
\addplot [
domain=-3:3,
samples=100,
color=blue,
]
{x^3 + 3};
\addlegendentry{$x^3 + 3$}
%
\addplot [
domain=-3:3,
samples=100,
color=green,
]
{x^3 - 3};
\addlegendentry{$x^3 - 3$}
\end{axis}
\end{tikzpicture}
\end{subfigure}
%\hfill
\begin{subfigure}[b]{0.3\textwidth}
\begin{tikzpicture}
\begin{axis}[
axis y line = middle,
axis x line = middle,
xlabel = $x$,
ylabel = {$f(x) = x^3$},
grid=major,
]
\addplot [
domain=-3:3,
samples=100,
color=red,
]
{x^3};
\addlegendentry{$x^3$}
\end{axis}
\end{tikzpicture}
\end{subfigure}
\caption{lajsdfls}
\end{figure}
There are two problems with your code.
First horizontal alignment of the figures was incorrect, but this can easily be fixed by using
\begin{subfigure}[b]{0.3\textwidth}
instead of
\begin{subfigure}[b!]{0.3\textwidth}
concerning the width, what is done when you create a subfigure environment is that a minipage of the indicated width is created. But is is up to you to respect this width with your content, no rescaling is done.
For instance, if, in a subfigure, you include an image and give it a width of \linewidth, the width will be respected. But if you give this image a 15cm width, probably it will be larger than you minipage. But LaTeX will respect your directives (and indicate an overfull hbox).
This is the problem that you have. Your plots are too large and overlap.
There are two ways to fix that.
You can give a width=\linewidth parameter to the axis environment, but it generally requires a redesign of your plot
you can rescale the box created by tikz. The most flexible way to do that is with adjustbox package
\documentclass{article}
\usepackage{subcaption}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{adjustbox}
\begin{document}
\begin{figure}
\centering
\begin{subfigure}[b]{0.45\textwidth}
%%% \begin{adjustbox}{width=\linewidth} % rescale box
\begin{tikzpicture}
\begin{axis}[
%%% width=\linewidth, % or modify the plot width
axis y line = middle,
...
...
\end{axis}
\end{tikzpicture}
%%% \end{adjustbox} %
\end{subfigure}%
\hfill
\begin{subfigure}[b]{0.45\textwidth}
etc.
Adding a width parameter to the axis environment
Rescaling with adjustbox
BTW, if you do not intend to add subcaptions to your plots, the subfigure environment is useless, and you can just put your (properly scaled) tikzpictures side by side separated by an \hfill.
I put two image.png in my latex documnet using \includegraphics. Now I want to joint two points of these images together by a line. I wonder if it is possible to do so in Latex?
First: no it is impossible: as Gyro Gearloose had said LaTeX has no idea about the content of your png.
Second: sure you can ;-) A little cheat with tikz. You can connect two arbitrary points with an arrow (or a line).
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{lipsum}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{tikzmark} % arrows in tex
\usetikzlibrary{arrows} % arrows in tex
\usetikzlibrary{calc} % (node)+(3cm,2cm)
\tikzstyle{every picture}+=[remember picture]
\begin{document}
\lipsum[1]
\includegraphics[width=0.2\textwidth]{solidtex.png}
{\tikz\node[coordinate](start1){};}
\lipsum[2]
{\tikz\node[coordinate](end1){};}
\includegraphics[width=0.2\textwidth]{solidtex.png}
\lipsum[3]
\begin{tikzpicture}[overlay, remember picture, -latex, color=blue!15!red, yshift=1ex, shorten >=1pt, shorten <=1pt, line width=0.1cm]
\path[->] (start1) edge [out=150, in=240] (end1);
\end{tikzpicture}
\end{document}
But it still connects the corners of the images. To connect points inside the pictures, you have to modify the coordinates manually e.g.:
\path[->] (start1) edge [out=150, in=240] ($(end1)+(2cm,2cm)$);
or pt, ex, mm ect...
If you want line without arrow head, use:
\path[-] (start1) edge [out=150, in=240] (end1);
Note that like labeling and referring figures, you have to run latex (or pdflatex or ...) twice.
For a small project I'm trying something that feels as if it should be peanut, but au contraire (Hey, that's french, never thought I'd remember some parts of that. anyway...).
The thing is, I want a latex layout where the header and footer span the entire pagewidth and have a background color, and also can have some elements on the left, center, or right (like with fancyhdr). The thing is, I simply cannot get this to work without screwing up the rest of the layout.
edit: added example.
Here's an example of what I have tried. The header is shown incorrectly since the background doesn't span the whole width of the page. The footer is not shown at all.
\documentclass[10pt,a4paper,notitlepage]{article}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage[usenames,dvipsnames]{color}
\usepackage{fancyhdr}
\usepackage{calc}
\usepackage{tikz}
\usepackage[landscape, top=0cm, bottom=3cm]{geometry}
\definecolor{logoGreen}{RGB}{218,226,87}
\newcommand{\HBG}{
\begin{tikzpicture}[remember picture,overlay]
\node[yshift=-2cm] at (current page.north west)
{
\begin{tikzpicture}[remember picture, overlay]
\draw[fill=logoGreen] (0,0) rectangle (\paperwidth,2cm);
\end{tikzpicture}
};
\end{tikzpicture}
}
\fancyhead{}
\fancyhead[L]{\HBG}
\fancyhead[C]{\includegraphics[height=1.2cm]{"img/headerimg"}}
\fancyfoot[C]{\includegraphics[height=1.2cm]{"img/footerimg"}}
\fancyhfoffset[]{1in+\hoffset+\oddsidemargin}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\headheight 3cm
\footskip -2cm
\pagestyle{fancy}
\begin{document}
\section{first section}
content
\end{document}
You can use TikZ to position coloured boxes absolutely on the page. See an example here.
This example includes usage of \thechapter, so you can probably go on from there.
For some reason I thought it would be nice to have inline figures (i.e. no floats) just like the equation environment. They would have to be numbered, as I want to be able to refer to them later on. I've come up with two attempts, but both have their shortcomings. I'm hoping for some feedback that can sort me out.
The first attempt uses 3 minipages (see the code below). This looks nice as the figure number is aligned vertically with the middle of the figure. However, as the width of the figure approaches the width of the page, things start to break down. Also, it doesn't behave very nicely at pagebreaks.
The second attempt uses the equation environment with a different label. Apart from the fact that I don't know if this is a sensible thing to do, it produces extra whitespace in the beginning of the next paragraph. It also doesn't align the label vertically in the center, but puts it on the bottom.
Here's an example of both attempts:
\documentclass{article}
\usepackage{pgf,tikz}
\usepackage{lipsum}
%
% Attempt 1
%
% Uses 3 minipages.
% Breaks if figure is wide, and at the bottom of a page.
%
\usepackage{calc}
\newlength{\figlabelwidth} % width of label
\newlength{\imgwidth} % max. width of figure
\newenvironment{inlinefig1}
{
\refstepcounter{figure} % increase figure number
\begin{center} % don't know if this is necessary
\setlength{\figlabelwidth}{\widthof{(Fig. \thefigure)}}
\setlength{\imgwidth}{\textwidth - \figlabelwidth - \figlabelwidth}
\setlength{\imgwidth}{0.9\imgwidth} % to be on the safe side
\begin{minipage}{\figlabelwidth}\makebox[\figlabelwidth]{}\end{minipage} % ghost minipage for centering
\hfill
\begin{minipage}{\imgwidth}\begin{center} % minipage for figure
}
{
\end{center}\end{minipage}
\hfill
\begin{minipage}{\figlabelwidth}(Fig. \thefigure)\end{minipage} % minipage for label
\end{center}
}
%
% Attempt 2
%
% Uses an equation environment with relabeled labels.
% Label is not centered vertically, and produces extra whitespace in the paragraph after it.
%
\def\theoldequation{\theequation} % save the old equation format
\newenvironment{inlinefig2}
{
\refstepcounter{figure} % increase figure number
\def\theequation{Fig. \arabic{figure}} % switch to figure numbering
\begin{equation}
}
{
\end{equation}
\def\theequation{\theoldequation} % reset to old equation label format
\addtocounter{equation}{-1} % correct the equation numbering
}
\begin{document}
\noindent \lipsum[1]
\begin{inlinefig1}
\begin{tikzpicture}
\draw (0,0) grid +(12,2);
\end{tikzpicture}
\end{inlinefig1}
\lipsum[2]
\begin{inlinefig2}
\begin{tikzpicture}
\draw (0,0) grid +(12,2);
\end{tikzpicture}
\end{inlinefig2}
\lipsum[3]
\end{document}
Do you guys have any better ideas, or suggestions to fix the any of the shortcomings? Thanks!
If you use the "float" package, you can specify H as a placement, which makes it appear exactly "here".
\newbox\inlinefigbox
\newenvironment{inlinefig3}
{
\refstepcounter{figure} % increase figure number
\setbox\inlinefigbox=\hbox\bgroup
}
{
\egroup
\hbox to \hsize{\hfil \box \inlinefigbox \hss (Fig. \arabic{figure})}
}