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

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 ;-).

Related

latex: how to "\input" a file whose filename depends on "~"?

in a latex file called "main.tex" i can write the line
\input /home/user1/defs.tex
to input the conents of "defs.tex" at the position of that command.
now i want to generalize this, so that any user can compile the tex-document and "defs.tex" inside his home-directory is being inserted. my first guess
\input ~/defs.tex
didnt work. does anyone know how to solve this?
https://tex.stackexchange.com/questions/1728/accessing-environment-variables-within-a-document
Thus, it would seem that:
\documentclass{article}
\immediate\write18{echo $HOME >/tmp/foo.tex}
\begin{document}
\input{var}
\end{document}
might do the trick. Or some such.
NOTE: I'm not a TeX expert, so be sure to read the referenced article carefully, and be prepared to experiment.

Conditional Import in LaTeX?

I'm going to be taking a ton of lecture notes, and then compiling them into LaTeX so that I can have excellent documents for future me to look over. I'm trying to organize things so that I can have a bunch of little documents containing the notes from a lecture, and then compile them at the end of the semester into one large document containing all of them. I have used import/include etc. successfully in the past, but I've had to remove the content at the head and foot of the sub-documents before compiling the main document. For example, I would have to remove:
\begin{document}
and
\end{document}
from every sub-document before compiling the main document. This is fine for a report with 5 or so sections, but a pain in the ass for something with 100+. Any recommendations for ignoring the contents of a LaTeX file programmatically when using the import command?
I see two approaches here. Either carefully structure your documents, or use some hacky TeX magic:
The smart way
Break your smaller documents into a header part, a footer part and a content part.
header.tex:
\documentclass{article}
...
\begin{document}
footer.tex:
\end{document}
foo-content.tex:
In this paper, we discuss an new approach to metasyntactic variables...
foo.tex (the small paper version):
\include{header}
\include{foo-content}
\include{footer}
In your .tex for the collected articles document:
\include{foo-content}
The hacky TeX way
Put this in some common include file, used by your individual files:
\ifx\ismaindoc\undefined
\newcommand{\inbpdocument}{\begin{document}}
\newcommand{\outbpdocument}{\end{document}}
\else
\newcommand{\inbpdocument}{}
\newcommand{\outbpdocument}{}
\fi
Use \inbpdocument and \outbpdocument in your individual files, in place of \begin{document} and \end{document}. In your main file, put in a \def \ismaindoc {} before including or importing anything.
Here's another possible approach: if you put magic strings (i.e., "% % BEGIN LECTURE % %" ... "% % END LECTURE % %") in the individual files, you could awk out the guts of the individual files, assemble them using make/sh, and then \include them.
There's another hack you could use, which wouldn't require modifying the individual files... just temporarily redefine the {document} environment (to something benign, i.e. a no-op), \include the individual files, and then restore the {document} environment definition.
If I recall correctly, the commands to do this are \let and \renewenvironment.
Hm. You might also have to temporarily redefine \documentclass and \usepackage, too. It's a hack, yes, but I think it should work.
I haven't used it, but it looks like the "subfiles" package does exactly what you want:
http://en.wikibooks.org/wiki/LaTeX/Modular_Documents#Subfiles_package

Can I make a LaTeX macro 'return' a filename?

I'm writing my thesis/dissertation and since its an on-going work I don't always have the actual images ready for the figures I put into my document, but for various reasons want to automatically have it substitute a dummy figure in place when the included graphics file doesn't exist. E.g. I can do something like \includegraphics[width=8cm]{\chapdir/figures/fluxcapacitor} (where \chapdir is a macro for my 'current' chapter directory, e.g. \def\chapdir{./ch_timetravel} and if there's no ./ch_timetravel/figures/fluxcapacitor.jpg it'll insert ./commands/dummy.jpg instead.
I've structured my macros (perhaps naïvely?) so that I have a macro (\figFileOrDummy) that determines the appropriate file to include by checking if the argument provided to it exists, so that I can call \includegraphics[properties]{\figFileOrDummy{\chapdir/figures/fluxcapacitor}}. Except I'm getting various errors depending on how I try to call this, which seem to suggest that I'm approaching the problem in a fundamentally flawed way as far as 'good LaTeX programming' goes.
Here's the macro to check if the file exists (and 'return' either filename or the dummy filename):
\newcommand{\figFileOrDummy}[1]{%
% Figure base name (no extension) to be used if the file exists
\def\fodname{#1}%
\def\dummyfig{commands/dummy}%
% Check if output is PS (.EPS) or PDF (.JPG/.PDF/.PNG/...) figures
\ifx\pdfoutput\undefined%
% EPS figures only
\IfFileExists{\fodname.eps}{}{\def\fodname{\dummyfig}}%
\else%
% Check existence of various extensions: PDF, TIF, TIFF, JPG, JPEG, PNG, MPS
\def\figtest{0}% flag below compared to this value
\IfFileExists{\fodname.pdf}{\def\figfilenamefound{1}}{\def\figfilenamefound{0}}%
\IfFileExists{\fodname.jpg}{\def\figfilenamefound{1}}{}%
\IfFileExists{\fodname.png}{\def\figfilenamefound{1}}{}%
% and so on...
% If no files found matching the filename (flag is 0) then use the dummy figure
\ifx\figfilenamefound\figtest%
\def\fodname{\dummyfig}%
\fi%
\fi%
% 'return' the filename
\fodname%
}%
Alternatively, here's a much simpler version which seems to have similar problems:
\newcommand{\figFileOrDummy}[1]{%
\def\dummyfig{commands/dummy}%
\dummyfig%
}
The \def commands seems to be processed after the expansion of the macro they're trying to define, so it ends up being \def {commands/dummy}... (note the space after \def) and obviously complains.
Also it seems to treat the literal contents of the macro as the filename for \includegraphics, rather than resolving/expanding it first, so complains that the file '\def {commands/dummy}... .png' doesn't exist..
I've tried also doing something like
\edef\figfilename{\figFileOrDummy{\chapdir/figures/fluxcapacitor}} to try to force it to make \figfilename hold just the value rather than the full macro, but I get an Undefined control sequence error complaining the variables I'm trying to \def in the \figFileOrDummy macro are undefined.
So my question is either
How do I make this macro expand properly?; or
If this is the wrong way of structuring my macros, how should I actually structure such a macro, in order to be able to insert dummy/real figures automatically?; or
Is there a package that already handles this type of thing nicely that I've overlooked?
I feel like I'm missing something pretty fundamental here...
I think the point is that \expandafter is only interested in its arguments as a string representing a filename, so doesn't evaluate it — macro languages are lazy! Try \expandafter {\includegraphics[width=8cm]}{\chapdir/figures/fluxcapacitor}.
Two points of style:
You don't need to put % at the end of a line to stop spurious whitespace if the line ends with a control sequence: the control sequence gobbles up all following whitespace, including the end of line. This makes the code much more readable, to my taste. Note that, in particular, to Tex's "mouth" both \def\newcs{abc} and \def \newcs {abc} are identical: they are exactly the same sequence of tokens.
I dropped the code around \figtest: you get better error reporting -always at a premium with Tex- if you use either \newif primitive (create new test with \newif\figexists, set/reset with \figexiststrue, \figexistsfalse, and test with \iffigexists...) or the Latex ifthenelse package (to keep with orthodoxy).
Cleaned-up code
I first thought the problem lay elsewhere, so wrote something prettier:
\def\dummypath{commands/dummy}%
\ifx\pdfoutput\undefined
\def\figFileOrDummy#1{\IfFileExists
{#1.eps}{#1}\dummypath}
\else
\def\figFileOrDummy#1{\IfFileExists
{#1.pdf}{#1}{\IfFileExists
{#1.jpg}{#1}{\IfFileExists
{#1.png}{#1}\dummypath}}} %or have more graphics types, if you like.
\fi
Alright, so I've found a possible answer to #2, by restructuring the way the macros work (and sort of using some suggestions from Charles Stewart's answer — I'll admit I don't like the 'look' of what seems to be widely considered good LaTeX code, I'm perhaps too ingrained in my C/C++ ways to be a real LaTeX programmer).
Anyway, my answer...
Instead of trying to produce the file name in a macro to pass to the \includegraphics macro, make a macro that wraps \includegraphics and passes it the real or dummy file name. This seems to avoid passing (as an argument) a long script/macro, though I don't see any good reason why it should have to be written this way. But it does work...
% Dummy figure file
\def\dummyfigure{commands/dummy}%
% Includegraphics wrapper macro to include either dummy or real figure
\ifx\pdfoutput\undefined
\newcommand{\incgfx}[2]{%
\def\testfile{\chapdir/fig/#2}%
\IfFileExists{\testfile.eps}%
{\includegraphics[#1]{\testfile}}% test file found
{\includegraphics[#1]{\dummyfigure}}% test file not found
}
\else
\newcommand{\incgfx}[2]{%
\def\figfilename{\dummyfigure}
\def\testfile{\chapdir/fig/#2}
\IfFileExists{\testfile.jpg}{\def\figfilename{\testfile}}{}
\IfFileExists{\testfile.png}{\def\figfilename{\testfile}}{}
\IfFileExists{\testfile.pdf}{\def\figfilename{\testfile}}{}
\IfFileExists{\testfile.jpeg}{\def\figfilename{\testfile}}{}
\IfFileExists{\testfile.tif}{\def\figfilename{\testfile}}{}
\IfFileExists{\testfile.tiff}{\def\figfilename{\testfile}}{}
\includegraphics[#1]{\figfilename}
}
\fi
This allows one to use it as intended:
\begin{figure}
\begin{center}
\incgfx{height=3cm}{\chapdir/fig/fluxcapacitor}
\caption{...}\label{fig:...}
\end{center}
\end{figure}
Again, I'd like to think there's a way to make the original idea work rather than having to make a wrapper for existing functions, but this will do for now...
Answer to #3: For this purpose, I find very useful the todonotes package. It does not provide the level of automation that your code is aiming to offer, but it has a very nice \missingfigure command that lets you put a dummy box for, you guess it, a missing figure.

Exclude entry from glossary?

I'm using the glossaries package in LaTeX. I've got \gls{foo} in my document, but I don't want the entry for "foo" to appear in the glossary. How can I keep a working (i.e. expanding) \gls{foo} in the body of my document, but exclude the entry for "foo" from the glossary?
EDIT: I want to use \gls{foo} to indicate "as used here, 'foo' has its specific meaning within this document." In a few cases, though, I've ended up with a "foo" whose definition is too obvious--or difficult--to articulate in the glossary.
So I want \gls{foo} to be expanded as usual, but I don't want the "foo" entry to appear in the glossary.
I hope this adds a little more information about what I'm trying to accomplish. It may be an abuse of glossaries, but I find it helpful to make sure I'm always using the same words and the right words while writing technical documents.
If you are using the glossaries package you can create an "ignored" glossary like
\documentclass{article}
\usepackage{glossaries}
\newglossary[glignoredl]{ignored}{glignored}{glignoredin}{Ignored Glossary}
\makeglossaries
\newglossaryentry{foofoo}{name={FOOFOO},description={foofoo stuff}}
\newglossaryentry{foo}{name={FOO},type={ignored},description={no good description}}
\newglossaryentry{bar}{name={BAR},description={bar of stuff}}
\begin{document}
Here is a \gls{foo} that is also a \gls{bar}, but of course it's also a \gls{foofoo}.
Why not consider buying a \gls{foo}?
\printglossary
% \printglossary[type={ignored}]
\end{document}
I have no idea why you'd want to do this, but the following should work:
\let\oldgls\gls% store the original meaning of \gls in a new command named \oldgls
\let\gls\relax$ make \gls do nothing
Some text with \gls{foo} no links to the glossary,
and no ``foo'' entry in the glossary.
\let\gls\oldgls% restore the original meaning of \gls
Some more text with \gls{bar} that links to the glossary,
and with a ``bar'' entry in the glossary.
This can be accomplished by adding the terms to a special common dictionary. It's actually a built-in feature of the glossaries package and it's even exemplified by the package author. From said example:
\documentclass{article}
\usepackage{glossaries}
\newignoredglossary{common}
\makeglossaries
\newglossaryentry{sample}{name={sample},description={an example}}
\newglossaryentry{commonex}{type=common,name={common term}}
\begin{document}
\gls{sample}. \gls{commonex}.
\printglossaries
\end{document}
Note the use of the \newignoredglossary command.

Problem in Latex: "There's no line here to end"

I'm using Lyx to produce a Latex document, and when i try to convert to pdf, it complains of the error: "there's no line to end", and description is "//".
My Latex document is like 200 lines without many line breaks. How am I supposed to debug this and get this darned pdf converted. I literally am stuck on this for hours, can't submit this pdf. And for 90% of the time while writing this document, the convert to pdf works fine, I don't know know since what point did it start to fail.
Someone give me a quick way to get rid of this error? Otherwise this Latex document is useless.
Thanks.
That may sound stupid, but sometimes Latex needs a sign before the \, therefore:
~\\
That very much depends on the distribution you chose. LyX btw. is kind of crazy, when it comes to LaTeX export. Try auctex if you've got a month or two to spare ;)
IMO this is the problem with LyX; when things go wrong it's hard to know where to look to fix the problem. This is a minimal document that shows an example of the error:
\documentclass{article}
\begin{document}
\\ there
\end{document}
I suggest exporting your LyX document to LaTeX, then compiling it "by hand" (with pdflatex mydoc or whatever) and see where in the document the error is appearing. You should then be able to correlate it with some misbehaving piece of the LyX document.
Do a binary search. Delete half the document, if it compiles then the problem was in the part you deleted. If not, then it's in the half you kept. Repeat the procedure on the offending portion and you should soon find which line is causing it.
Dear Saobi, please post the offending line. Probably you have a "\" in a single-line math environment. If you post the code for the complete environment, I can try to indicate how to prevent this error.
You can go to View -> View Source and click around until you find the offending line. In latex, line break is \\.
After doing that I realized the problem is you can't put a line break (Ctrl+Enter) at the start of a line (or cell). You can cheat by forcing a space (Ctrl+Space) before the line break. :)
My approach for locating compilation errors in LaTeX documents is based on a binary search approach. I suppose that a similar approach can be used in LyX.
The key idea is to divide your document in two parts of approximately the same size. At the boundary between these two parts a line containing \end{document} is inserted. If the document now can compile with no problems, the problem were located in the second half part of the document (otherwise it were in the first half part).
\documentclass{article}
\begin{document}
% First half part of the document
\section{Hello}
% Location for inserting \end{document}
% Second half part of the document
% in which the error is located
\section{World
\end{document}
In addition, check out that your LaTex lines already have content. I wrote an empty line, and It also causes error, from my experience.
\vspace{1cm}{ }\\

Resources