Making a small modification to a LaTeX environment - latex

I've been using \begin{figure} ... \end{figure} throughout my LaTeX document, but the default styling is ugly; namely, the figures are all left-aligned. Is there a way to redefine the "figure" environment so it automatically inserts some centering commands such as like this?:
\begin{figure} \begin{center}
\end{center} \end{figure}
Sure, I could use \newenvironment to define a "cfigure" environment, but that's undesirable. I don't want to go through and change all my "figures" to "cfigures" (and then later realise I wanted all the figures to be right-aligned and have to rename them all to "rfigures").
I could use \renewenvironment, but then I'd have to dig through the LaTeX source to find what the "figure" environment was originally defined as copy/paste it in.
I almost found what I wanted at this blog post, but the example there was for a command, not an environment.

\let\oldfigure\figure
\def\figure{\oldfigure\centering}
Another solution which works with the optional arguments.
Fixed.
\let\oldfigure\figure
\let\oldendfigure\endfigure
\def\figure{\begingroup \oldfigure}
\def\endfigure{\centering \oldendfigure \endgroup}
Fixed 2. It does work well with any options and any rules and \par inside.
\makeatletter
\let\oldfigure\figure
\def\figure{\#ifnextchar[\figure#i \figure#ii}
\def\figure#i[#1]{\oldfigure[#1]\centering}
\def\figure#ii{\oldfigure\centering}
\makeatother

As noted in another answer, you can't do the old trick of prepending commands to the end of the \figure macro because that will mess up the optional argument processing.
If an environment doesn't have arguments then it will work fine, but otherwise there's no straightforward way of doing this.
For your problem with the figures, try loading the floatrow package:
\usepackage{floatrow}
If will centre the content of your figures automatically.
Update: If you don't want to load a package, here's some code that will also do it. Note that it's specific to the figure environment, but the basic theme is: copy the original definition, parsing arguments the same way, then add whatever code you need at the end.
\makeatletter
\renewenvironment{figure}[1][\fps#figure]{
\edef\#tempa{\noexpand\#float{figure}[#1]}
\#tempa\centering
}{
\end#float
}
\makeatother
The \edef is required to fully expand \fps#figure before it's passed to the \#float macro.

How about:
\newenvironment{centeredfigure}{\begin{figure}\begin{center}}{\end{center}\end{figure}}
Note: untested.

Related

Treating a quote block similarly to a figure in LaTeX?

Been trying to figure out how to achieve this but can only find ways to do it within the figure environment using wrapfig. I'm using the epigraph package to generate the quote style (as in the screenshot). The current behavior is in this screenshot and generated by the code
\subsection{Methods}
\lipsum[1-2]
\epigraph{Models, of course, are never true, but fortunately it is only necessary that they be useful.}{\textcite{Box1979-em}}
However, the below image is how I wish it to look, but unsure how to achieve this?
Without a MWE, I had to make some adjustments; but this appears to work.
\documentclass[11pt,letterpaper]{article}
\usepackage{lipsum}
\usepackage{epigraph}
\usepackage{biblatex}
\usepackage{wrapfig}
\begin{document}
\setcounter{section}{2}
\subsection{Methods}
\begin{wrapfigure}{r}{0pt}%
\begin{minipage}{0.43\linewidth} % manual adjustment required
\epigraph{Models, of course, are never true, but fortunately it is only necessary that they be useful.}{Box (1979)}
\end{minipage}
\end{wrapfigure}
\lipsum[1-2]
\end{document}

How do I ref a figure in LaTeX before it occurs?

I always like my figures to be placed in between text as opposed to the top or bottom of the page. I also like to talk about the figure before it is shown. So I am trying to have something like this:
By looking at Figure~\ref{fig:VCO} you can see that blah blah blah.
\begin{figure}[h]
\caption{VCO test circuit}\label{fig:VCO}
\begin{center}
\includegraphics[width=0.9\columnwidth]{figures/VCO_circuit.eps}
\end{center}
\end{figure}
This doesn't seem to work because it I guess it is referencing something that hasn't occurred yet? Does anyone have some simple solution? I am still very new to LaTeX.
Generally LaTeX needs at least two passes to resolve all its references, the first time to write them to an auxiliary file and the second time to put them into the final ps/pdf/dvi file. So it does not matter where the reference is.
A third pass will be needed, for example, if your document has a long table-of-contents which will screw up page numbers.
It failed the first time because labeling and referencing are a two-pass process. The first time you processed your latex, all the labels were being indexed so the ref failed. The second time around, since the labels had been indexed the ref knew what it was actually referencing.
I would add that latexmk (link) has proven invaluable to me over the years. This is a LaTeX "build" script written in Perl that is designed to compile .tex source files the right number of times. It parses the output from the latex command and performs dependency checking to ensure that the output document is kept up-to-date with the minimum number of passes. It can also deal with BibTeX bibliography files. Generally speaking, I invoke latexmk from either an Ant or GNU Make makefile and treat it just like I'm compiling C++ code, for example.
I had same problem and I found this solution:
\graphicspath{{images/}}
\DeclareGraphicsExtensions{.jpg}
\makeatletter
\newenvironment{tablehere}
{\def\#captype{table}}
{}
\newenvironment{figurehere}
{\def\#captype{figure}}
{}
\makeatother
\begin{figurehere}
\includegraphics[height=5cm]{2-14aGa-Sur.jpg}
\caption{Hliněná destička s mapou severu Mezopotámie}
\label{fig:Ga-Sur}
\end{figurehere}
\graphicspath{{images/}} is there to declare your path to your pictures
\DeclareGraphicsExtensions{.jpg} is there for declare picture extension (multiple can be with comma (I think ;-))
\makeatletter
\newenvironment{tablehere}
{\def\#captype{table}}
{}
\newenvironment{figurehere}
{\def\#captype{figure}}
{}
\makeatother
is there for precise determination of position here
\begin{figurehere}
\includegraphics[height=5cm]{2-14aGa-Sur.jpg}
\caption{Hliněná destička s mapou severu Mezopotámie}
\label{fig:Ga-Sur}
\end{figurehere}
there is your picture with height specified and caption and label with it...
I hope it will help you ;-).

glossaries package and footnote in LaTeX

I am currently stuck, having two separate glossaries: main & acronyms. Acronyms glossary prints footnotes on first use in the text, but main glossary does not. Is there any way to make any other glossary than acronyms to print footnote on first use of the term? I don't get how to do it.
Here is the code example compiled with TeXnic Center and MiKTeX 2.7:
\documentclass{article}
\usepackage{index}
\usepackage[toc,style=long3colheaderborder,footnote,acronym]{glossaries}
\makeindex
\makeglossaries
\newglossaryentry{appdomain}{name={application domain}, description={app Domain Description...}}
\newglossaryentry{sample}{name={[has been inserted aaa]},description={testing testing 123}}
\newacronym{aca}{aca}{a contrived acronym}
\begin{document}
\section{this is a test section}
This is the test line... a \gls{sample} \gls{appdomain}
\index{entry} and \gls{aca}
\thispagestyle{empty}\cleardoublepage
\printglossary[type=main,title={Glossary},toctitle={Glossary}]
\thispagestyle{empty}\cleardoublepage
\printglossary[type=\acronymtype,title={List of Abbreviations},toctitle={List of Abbreviations}]
\printindex
\thispagestyle{empty}\cleardoublepage
\end{document}
I want sample and appdomain either contain a footnote with description or a footnote stating: please refer to Glossary
Many thanks,
Ovanes
In short, with the glossaries package, you can't get footnotes on the first use for non-acronym glossaries.
However, you can redefine some commands in the preamble (after you \usepackage{glossaries}) to get what you want:
\makeatletter
\renewcommand{\gls#main#displayfirst}[4]{
#1#4\protect\footnote{#2}
}
\makeatother
But that will be really fragile.
I think there is an easier way of doing this. Maybe it's new, but
\defglsdisplayfirst[main]{#1#4\protect\footnote{#2}}
appears to achieve the exact same thing (correct me if I'm wrong). See the glossaries manual, version 2.03, subsection 2.4.1 changing the format of the text.
Unfortunately, it appears commands like \gls or \autoref does not work in those footnotes.
Following is a good technique, to put a footnote stating where the definitions are:
\label{nom} %put this on the page your term appears, so that it can collect page number
\newcommand{\g}{\footnote{For all abbreviations see the glossary on page \pageref{nom}.}}
I've found this from here.

How to customize an existing LaTeX environment without interfering with other environments?

I'm using Sphinx for documenting a project. It produces LaTeX files from reStructuredText.
I would like to set a gray background color to the tips and notes, so I customized the notice environment after creating a graybox environment:
\definecolor{MyGray}{rgb}{0.80,0.80,0.80}
\makeatletter\newenvironment{graybox}{%
\begin{lrbox}{\#tempboxa}\begin{minipage}{\columnwidth}}{\end{minipage}\end{lrbox}%
\colorbox{MyGray}{\usebox{\#tempboxa}}
}\makeatother
\makeatletter
\renewenvironment{notice}[2]{
\begin{graybox}
\bf\it
\def\py#noticetype{#1}
\par\strong{#2}
\csname py#noticestart##1\endcsname
}
{
\csname py#noticeend#\py#noticetype\endcsname
\end{graybox}
}
\makeatother
Everything works fine except if I place a figure environment inside the notice environment. In that case, I get this error:
LaTeX Error: Not in outer par mode
Is there a way to set a gray background to that notice environment ?
This is a FAQ. It does not make sense to put a figure (or any other "float" which can move elsewhere in the output) inside a gray box; if you want your figure to contain a gray box, put the gray box environment inside the figure environment.
Thank you godbyk and Jouni for answering my question.
The problem is that I don't code directly in LaTeX. I write the documentation in restructured text and Sphinx output the LaTeX files.
But I found a solution: I redefine the figure environment to use the staticfigure from the flowfram package:
\usepackage{flowfram}
\definecolor{MyGray}{rgb}{0.80,0.80,0.80}
\makeatletter\newenvironment{graybox}{%
\begin{lrbox}{\#tempboxa}\begin{minipage}{\columnwidth}}{\end{minipage}\end{lrbox}%
\colorbox{MyGray}{\usebox{\#tempboxa}}
}\makeatother
\makeatletter
\renewenvironment{notice}[2]{
\begin{graybox}
\bf\it
\def\py#noticetype{#1}
\par\strong{#2}
\csname py#noticestart##1\endcsname
}
{
\csname py#noticeend#\py#noticetype\endcsname
\end{graybox}
}
\makeatother
\renewenvironment{figure}[6]{
\begin{staticfigure}
}{
\end{staticfigure}
}
PS: I had to put 6 to the number of arguments when redefining 'figure': if I don't do that it outputs some 'htbp' in the pdf files (I'm not a LaTeX expert. it's just the solution I found for this problem)
As Jouni correctly pointed out, figures and tables (i.e., floats) can be moved around, and your gray box can't contain them. To achieve the desired effect, you have two options:
Put your entire notice into a figure environment (so that the entire notice can be floated around on the page or to a new page if LaTeX so chooses).
Don't use a float (figure environment) -- just use \includegraphics to pop your image directly into the notice environment. You won't be able to use a caption with this non-figure, however, as captions only work inside a figure or table environment. If you want a caption associated with this image, you can use the caption package:
\documentclass{article}
\usepackage{caption}% let's us use captions outside of floats
\usepackage{lipsum}% provides filler text
\begin{document}
\lipsum[1]
\begin{center}
\includegraphics{mypic}
\captionof{figure}{This is my picture.}% makes a caption for non-floats
\label{fig:mypic}
\end{center}
\lipsum[2]
\end{document}
I haven't used Sphinx, so I'm afraid I can't help you too much with integrating this into their output.

How do you display straight quotes instead of curly quotes when using LaTeX's 'listings' package?

I'm using LaTeX's "listings" package to format source code. Unfortunately I get curly quotes instead of straight quotes. Since the curly quotes don't always point in the right direction, it looks bad. How can I get straight quotes instead?
I'd prefer not to change or filter the source code itself. Filtering the code to properly change " to `` or '' would work, but this is easier done than said with multiple quotes on a line, or quotes spanning multiple lines. Or you could use symbol or a host of other things. But I'd really like to keep the source unchanged.
Example LaTeX:
\documentclass{article}
\usepackage{listings}
\begin{document}
\begin{lstlisting}
Fahrenheit=input("What is the Fahrenheit temperature?")
Celsius=(5.0/9.0)*(Fahrenheit-32)
print"The temperature is",Celsius,"degrees Celsius"
\end{lstlisting}
\end{document}
Example output (using Miktex on windows):
(Direct link to image of incorrect output)
I see in the documentation (which should have been distributed with the packge, but is available at http://www.ctan.org/tex-archive/macros/latex/contrib/listings/listings.pdf) for listings that there is a settable property called upquote to take care of this.
From the documentation:
upquote=⟨true|false⟩ false
determines whether the left and right quote are printed ‘’ or `'. This
key requires the textcomp package if true.
Do something like
\lstset{upquote=true}
before begining the list environment, or use
\begin{lstlisting}[upquote=true]
...
\end{lstlisting}
It is also possible that tis property is already set for you in the appropriate language
definition (see the docs again, big list of predefined languages on page 12).
Use:
\lstloadlanguages{<dialects you need>}
in the header. And then set the language using either of the above conventions for choosing options.
Have you considered using a monospaced (typewriter) font for the listing? The following example works:
\documentclass{article}
\usepackage{listings}
\lstset{basicstyle=\ttfamily} % <<< This line added
\begin{document}
\begin{lstlisting}
Fahrenheit=input("What is the Fahrenheit temperature?")
Celsius=(5.0/9.0)*(Fahrenheit-32)
print"The temperature is",Celsius,"degrees Celsius"
\end{lstlisting}
\end{document}
dmckee's answer above probably works. If you drop your last condition, i.e. you permit changes to the code, then there is a more generic solution, which I tend to use whenever (La)TeX renders a character somehow differently than I expect it to do is to use the \symbol command. I list it here because it can be useful in other situations as well:
\newcommand{\qq}{\symbol{34}} % 34 is the decimal ascii code for "
And then your example:
\begin{lstlisting}
...
print{\qq}The temperature is{\qq},Celsius,{\qq}degrees Celsius{\qq}
...
\end{lstlisting}
Note the curly braces which supposedly take listings back to LaTeX mode (see escapechars option of the package.)
Here is a solution
\usepackage[T1]{fontenc}
\usepackage{textcomp}
\usepackage{lmodern}
% in the listings package configuration, try:
literate={"}{\textquotedbl}1,
I had the same problem, using fontspec, and the solution was to not set \defaultfontfeatures{Mapping=tex-text}, but instead setting Mapping=tex-text specifically on only the main and sans font, and leaving the tt font to it's own devices. :)
Maybe it's because I installed listings early as a LaTeX user, but I'm surprised to learn that without the listings package the behaviour is any different.
My solution was similar to David Hanak's, but I used the symbols for double-quote as described in the LaTeX Cheat Sheet (http://stdout.org/~winston/latex)
\newcommand{\QQ}[1]{``#1''}

Resources