Inline figures with equation-like numbering in LaTeX - latex

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})}
}

Related

Two equal-height **minipages** in Latex Beamer?

I was looking for ways to have a side-by-side minipages on a Latex beamer.
I will be clear, this is not two columns or mult-column or whatever. I specifically need minipages.
To do so, I created a nice command/function which does it nicely, but I am trying to make the two miniapges always be equal in height as well.
Any suggestions on how to force them to be equal in size?
Also general improvement suggestions and tips are welcomed.
\def \MinSideBySideGap {0.02} % minimal Gap between left/right sides
\newcommand{\SideBySide}[3][0.5]
{
%
\ifthenelse{\isempty{#1}}%
{\FPeval{\leftwidth}{0.5-\MinSideBySideGap/2}%
\FPeval{\rightwidth}{\leftwidth}%
}% if #1 is empty
{\FPeval{\leftwidth}{min(#1-\MinSideBySideGap/2,1.0)}%
\FPeval{\rightwidth}{max(1.0-\leftwidth-\MinSideBySideGap,0.0)}
}% if #1 is not empty
%
% Left Part
\begin{minipage}{\leftwidth\textwidth}
#2
\end{minipage}%
\hfill%
% Right Part
\begin{minipage}{\rightwidth\textwidth}
#3
\end{minipage}%
}
Towards #samcarter_is_at_topanswers.xyz questions, this was the very old origin of this:
\newcommand{\SideBySide}[2]
{
\begin{columns}[T] % align columns
\begin{column}{.48\textwidth}
#1
\end{column}%
\hfill%
\begin{column}{.48\textwidth}
#2
\end{column}%
\end{columns}
}
I still don't see a reason why you would need minipages of the same height, but just as a proof of concept, you could use tcoloboxes instead of minipages. They have the ability to form "equal height groups":
\documentclass{beamer}
\usepackage[most]{tcolorbox}
\tcbset{width=(\linewidth-4mm)/2,before=,after={},enhanced,interior empty,equal height group=\insertframenumber,frame hidden}
\begin{document}
\begin{frame}
\begin{tcolorbox}%
test
\end{tcolorbox}%
\hfill%
\begin{tcolorbox}%
test
test
\end{tcolorbox}%
\end{frame}
\end{document}
(for the image, I removed the interior empty option so one can see the height of the box)

latex subfigure not centered

My code is as following but my figures are not centre aligned with captions. How can I fix it?
code result
\begin{figure}[H]
\centering
\begin{subfigure}{0.7\textwidth}
\includegraphics[width=4cm,height=6cm]{outwithsel.PNG}
\caption{Outliner}
\label{fig:h1}
\end{subfigure}
\begin{subfigure}{\textwidth}
\includegraphics[width=0.8\linewidth]{moveBtns.PNG}
\caption{Buttons that move selected object in 4 different directions}
\label{fig:h2}
\end{subfigure}
\caption{Kitchen is selected and can be moved to where desired}
Here somaia, that's what happens when I tried what you said. fix from comment 1
Your images have the widths 4cm and .8\textwidth, respectively. This means they are smaller than the subfigure around them. To get them centred inside the bigger subfigure, you need to repeat \centering inside the subfigure.
In addition there are missing % at the end of some of your lines. These unprotected line breaks will act like a space, and thus decentre your subfigure by a small amount (probably not noticeable, but when we are already at it...).
Unrelated to your problem: don't specify both the width and the height of the image, this will distort it. At least add keepaspectratio if you really must give both dimensions.
\documentclass{article}
\usepackage{subcaption}
\usepackage{graphicx}
\usepackage{float}
\begin{document}
\begin{figure}[H]
\centering
\begin{subfigure}{\textwidth}
\centering
\includegraphics[width=4cm,height=6cm]{example-image-duck}
\caption{Outliner}
\label{fig:h1}
\end{subfigure}%
\begin{subfigure}{\textwidth}
\centering
\includegraphics[width=.8\textwidth]{example-image-duck}
\caption{Buttons that move selected object in 4 different directions}
\label{fig:h2}
\end{subfigure}%
\caption{Kitchen is selected and can be moved to where desired}
\end{figure}
\end{document}
I think you have a problem in \texwidth, you have to choose the 0.5 textwidth for each subfigure
This will solve your problem:
\begin{figure}[H]
\centering
\includegraphics[width=.7\textwidth]{Bloch sphere.PNG}
\caption{Outliner}
\label{fig:h1}
\includegraphics[width=0.7\textwidth]{Bloch sphere.PNG}
\caption{Buttons that move selected object in 4 different directions}
\label{fig:h2}
\caption{Kitchen is selected and can be moved to where desired}
\end{figure}

text over figure in latex

I am trying to add a simple figure into my latex code .I have this code :
\begin{figure}[H]
\resizebox{40pt}{!}{
\centering \includegraphics{FigureStereo.jpg}}
\caption{Epipolar Geometry}
\label{fig:cclogo}
\end{figure}
with this however the text comes on top of the image and also image is not centered .What am I doing wrong?Hopefully this is not a repeated question coz I did do a quick check on related posts.
Edited
Here is the preamble
\documentclass[draftthesis,tocnosub,noragright,centerchapter,12pt,]{uiucecethesis09}
\makeatletter
\usepackage{setspace}
\usepackage{epsfig} % for figures
\usepackage{graphicx} % another package that works for figures
%\usepackage{subfigure} % for subfigures
\usepackage{amsmath} % for math spacing
%\usepackage{amssymb} % for math spacing
%\usepackage{url} % Hyphenation of URLs.
\usepackage{lscape} % Useful for wide tables or figures.
\usepackage[justification=raggedright]{caption} % makes captions ragged right - thanks to Bryce Lobdell
\usepackage{float}
\usepackage{wrapfig}
% Uncomment the appropriate one of the following four lines:
\msthesis
%\phdthesis
%\otherdoctorate[abbrev]{Title of Degree}
%\othermasters[abbrev]{Title of Degree}
\title{}
\author{}
\department{}
\degreeyear{}
% Advisor name is required for
% - doctoral students for the ProQuest abstract
% - master's students who do not have a master's committee
\advisor{}
% Uncomment the \committee command for
% - all doctoral students
% - master's students who have a master's committee
%\committee{Professor Firstname Lastname, Chair\\
% Professor Firstname Lastname} % etc.
You can use a center environment for centering, wrapping it around both the image box and the caption:
\begin{figure}[h!]
\begin{center}
\resizebox{40pt}{!}{\includegraphics{FigureStereo.jpg}}
\caption{Epipolar Geometry}
\label{fig:cclogo}
\end{center}
\end{figure}
Note, though, that LaTeX can sometimes be pretty stubborn about how it lays out pages where floating elements are concerned. The h option is more of a guideline than a guarantee. In some cases you might be better off not using a floating figure environment and instead inserting the graphics and text directly.
Also, you're better off asking this sort of question over at the TeX StackExchange site.
You probably just want something like
\begin{figure}[t]
\centerline{\includegraphics[width=\columnwidth]{figs/foo}}
\caption{Caption Goes Here}
\label{fig:cclogo}
\end{figure}

Problem with pgfplot label

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.

Latex: center text within listing

How do I center text within a listing in Latex?
Or another way not using listing. (Need a box with monospace font)
The answer given at tex.stackexchange is:
\documentclass{article}
\usepackage{listings}
\renewcommand{\figurename}{Listing}
% replace figurename with the text that should preceed the caption
\begin{document}
\begin{figure}[thp] % the figure provides the caption
\centering % which should be centered
\caption{Ausgabe des C-Programms}
\begin{tabular}{c} % the tabular makes the listing as small as possible and centers it
\begin{lstlisting}[label={gtt_c_ausgabe}]
printf("Your code goes here.\n");
\end{lstlisting}
\end{tabular}
\end{figure}
\end{document}
...which still leaves me wondering:
Using a frame around the code, e.g. using \lstset{frame=single,frameround=tttt}, places the frame way over to the right. How can this be avoided?
What does the renewcommand bit do?
I don't have an answer for the listing package on top of my head, but you could try the following:
\framebox[.9\linewidth]{\parbox{.85\linewidth}{\tt Hello World\\Second line}}
That produces a box with 90% of the line width, with text of width 85% of line width.
If you want it centered you just put \centering in front of the \tt command:
\framebox[.9\linewidth]{\parbox{.85\linewidth}{\centering \tt Hello World\\Second line}}
If you prefer the box without a frame, simply change \framebox into \makebox (and keep the arguments as they stand).

Resources