This is driving me crazy.
I want to center a lstlisting in LaTeX.
After 3 hours attempting here's some code:
\lstset{ %
caption=Descriptive Caption Text,
label=lst:descr_capti_text,
basicstyle=\ttfamily\footnotesize\bfseries,
frame=tb,
linewidth=0.6\textwidth
}
\centering\begin{tabular}{c}
\begin{lstlisting}
printf("this should be centered!");
\end{lstlisting}
\end{tabular}
This is putting the lstlisting on the center but not its caption, that goes to the right. If I take out the tabular, then caption gets centered but the code goes to the left! :(
Thank you.
Instead of using linewidth you should consider to use xleftmargin and xrightmargin (cf. texdoc listings, Chapter 4.10). The following code works without any center or minipage environment:
\lstset{
caption=Descriptive Caption Text,
basicstyle=\footnotesize, frame=tb,
xleftmargin=.2\textwidth, xrightmargin=.2\textwidth
}
\begin{lstlisting}
printf("this should be centered!");
\end{lstlisting}
Your caption is actually centered over the listing. You are just making the lines that run along the top and bottom of your listing only 0.6\textwidth long. This makes it appear as if the caption was off-center. Also, your \centering doesn't center the listing (visible if you don't shorten the lines below and above).
This should work:
\begin{center}
\lstset{%
caption=Descriptive Caption Text,
basicstyle=\ttfamily\footnotesize\bfseries,
frame=tb
}
\begin{lstlisting}
printf("this should be centered!");
\end{lstlisting}
\end{center}
You don't explain why you want the delimiting lines to be 0.6\textwidth long. If
you actually wanted to set the width of your listing to be that value, your
approach doesn't do what you want. Use something like a minipage to set a width
for the whole listing.
\begin{minipage}{0.6\textwidth}
\begin{center}
\lstset{%
caption=Descriptive Caption Text,
basicstyle=\ttfamily\footnotesize\bfseries,
frame=tb,
}
\begin{lstlisting}
printf("this should be centered!");
\end{lstlisting}
\end{center}
\end{minipage}
Actually, what did work for me is the converse: putting the minipage inside the center environment.
Related
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}
I am trying to put notes in the pictures of my work but the notes are getting aligned with the border of the text and not the figure. How to align the note (Fonte: Elaborated by the author (2021)) of the table with the left side of the figure?
\begin{figure}[h]
\begin{center}
\includegraphics[scale=.35]{image.jpg}
\caption{Text tex text.}
\label{fig_intbus}
\end{center}
\fonte{Elaborated by the author (2021)..}
\end{figure}
I would like something similar to the threeparttable environment for tables but that can be used in figures. I thought about the minipage environment but I don't want to specify the width for each figure manually.
Here is an example of how to do it, defining a new caption.
\documentclass{article}
\usepackage{graphicx}
\usepackage{caption}
\newcommand{\fonte}[1]{\captionsetup{skip=0.5ex,position=b}\caption*{\textit{Fonte:} {#1}}}
\begin{document}
\begin{figure}
\centering
\includegraphics[width=0.5\textwidth]{example-image-a}
\caption{Example image.}
\label{fig-img-a}
\fonte{Teste.}
\end{figure}
\end{document}
And the result is:
So i'm trying to hide the label in my cover image.
>\begin{figure}
>\center
>\includegraphics[scale=0.5]{universidade}
>\caption* {Mycaption}
>\end {figure}
This way it's not labeling the figure but its not showing on the list of figures
Help please ;D
You can give the caption package the option labelformat=empty to suppress the Figure 1 etc. labelling:
\usepackage[labelformat=empty]{caption}
You can use square brackets to specify the description for the figure list, and curly brackets for the actual figure caption. So I think you should be able to do the following to supress the caption but still have an entry in the figure list:
\begin{figure}
\center
\includegraphics[scale=0.5]{universidade}
\caption[Mycaption]{}
\end {figure}
There are a couple of options, depending on what yo're after exactly:
\documentclass{article}
\usepackage{graphicx}
\setcounter{topnumber}{3}% Just for this example
\begin{document}
\listoffigures
\begin{figure}
\addcontentsline{lof}{figure}{Example image A}%
\centering
\includegraphics[height=4\baselineskip]{example-image-a}
Example image A
\end{figure}
\begin{figure}
\addcontentsline{lof}{figure}{\protect\numberline{}Example image B}%
\centering
\includegraphics[height=4\baselineskip]{example-image-b}
Example image B
\end{figure}
\begin{figure}
\centering
\includegraphics[height=4\baselineskip]{example-image-c}
\caption{Example image C}
\end{figure}
\end{document}
The figure caption is added using \addcontentsline{lof}{figure}{<caption>}, where <caption> can either contain a blank \numberline{}, or just the regular caption. The above example shows the usage of either.
It would also be possible to have no label shows in the image, but have a numbered entry in the LoF using caption. But it would seem strange to have a numbered entry in the LoF and an unnumbered figure.
How do you have a figure imported so that it appears on the bottom of a page with a caption? When I import it and put a caption on it normally, it always appears at the top of a page.
\begin{figure}[b]
your figure
\end{figure}
See this link, for more syntax:
http://en.wikibooks.org/wiki/LaTeX/Floats,_Figures_and_Captions#Figures
Just an example:
Caption below
\begin{figure}[h]
\centering
\includegraphics[width=0.3\textwidth]{thatcat.jpg}
\caption{That cat!} % caption line
\end{figure}
Caption above
\begin{figure}[h]
\centering
\caption{That cat!} % caption line
\includegraphics[width=0.3\textwidth]{thatcat.jpg}
\end{figure}
To add to freyrs:
If if you put \caption above the \includegraphics the caption will be above the figure, if you put \caption below the \includegraphics the caption will appear below the figure.
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).