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

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.

Related

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

latex undefined control sequence with tableofcontents

I was using \documentclass{amsmath} for awhile with no issues, but I recently wanted to switch to the normal \documentclass{article} because I thought it looked nicer. However, now my \tableofcontents command produces this error:
! Undefined control sequence.
<argument> \tocsection
{}{1}{Purpose}
l.1 ...ne {section}{\tocsection {}{1}{Purpose}}{4}
?
I'm writing a research paper with latex, and Purpose is the first section (the command I'm using is \section{Purpose}). It's saying line 1 has the error, which is \documentclass{article}. Can anyone help me with this error?
1) Most probably "l.1" refers to the first line of the TOC file, not of the TEX file. It's impossible to tell for sure from the little snippet you've shown.
2) First thing to try in this situation will be to delete (or better to move to some temp. place) all the TeX-generated files in your working folder. Usually this means that only TEX and BIB files should remain (and any figures if you have them). Then run the usual latex; bibtex; latex; latex sequence.
3) If you still get the same error, there is probably some package conflict. To diagnose that, we'll need all of your preamble.

Making a small modification to a LaTeX environment

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.

Why does LaTeX start a new line when making a reference

I just created a LaTeX document and compile it to a PDF using pdftex. Every time I make a reference using the \ref command, the reference is put on a new line. Why does this happen and how do I prevent this?
Are you by any chance using \\ in URLs instead of //? \\ is used as a linebreak in TeX.
Try to localize the problem: create a new file which initializes hyperref the same way, uses no other packages, and has some text and a reference. Check if references still start on new lines. If they do, you can show it here (and on LaTeX forums); if not, add more of your report's preamble until you get a file which exhibits the problem.
Can you prepare a minimum example demonstrating your problem?
I've tried with:
\documentclass{report}
\usepackage[pdftex,breaklinks=true,bookmarks=true,bookmarksopen= true,pdfpagemode=UseNone,plainpages=false,pdfpagelabels,pdfstartview=FitH,olorlinks=true,citecolor=blue]{hyperref}
\begin{document}
Test \url{http://test.com} \ref{http://someother.com}.
\end{document}
And the whole output is a single line in PDF.
BTW \ref is used to produce internal document links (like in "As described in chapter \ref{chapter:second}...").
It was my own fault, I used \par for the paragraph sign instead of \P. This is one of my first LaTeX documents, so I overlooked it. Thanks for the quick responses/answers though.

Replace a character with a string in LaTeX

I'm looking for a way to do a substring replace on a string in LaTeX. What I'd like to do is build a command that I can call like this:
\replace{File,New}
and that would generate something like
\textbf{File}$\rightarrow$\textbf{New}
This is a simple example, but I'd like to be able to put formatting/structure in a single command rather than everywhere in the document. I know that I could build several commands that take increasing numbers of parameters, but I'm hoping that there is an easier way.
Edit for clarification
I'm looking for an equivalent of
string.replace(",", "$\rightarrow$)
something that can take an arbitrary string, and replace a substring with another substring.
So I could call the command with \replace{File}, \replace{File,New}, \replace{File,Options,User}, etc., wrap the words with bold formatting, and replace any commas with the right arrow command. Even if the "wrapping with bold" bit is too difficult (as I think it might be), just the replace part would be helpful.
The general case is rather more tricky (when you're not using commas as separators), but the example you gave can be coded without too much trouble with some knowledge of the LaTeX internals.
\documentclass[12pt]{article}
\makeatletter
\newcommand\formatnice[1]{%
\let\#formatsep\#formatsepinit
\#for\#ii:=#1\do{%
\#formatsep
\formatentry{\#ii}%
}%
}
\def\#formatsepinit{\let\#formatsep\formatsep}
\makeatother
\newcommand\formatsep{,}
\newcommand\formatentry[1]{#1}
\begin{document}
\formatnice{abc,def}
\renewcommand\formatsep{\,$\rightarrow$\,}
\renewcommand\formatentry[1]{\textbf{#1}}
\formatnice{abc,def}
\end{document}
it looks like your "spaces" problem is from a bug in that package. If you surround the "\GetTokens" macro with, say, commas, then you'll see that the extra space is inserted by that macro.
Yes there are bugs in tokenizer package. As I said on my blog, the bugfix is to use the following correcting code instead of just "\usepackage[trim]{tokenizer}":
\usepackage[trim]{tokenizer}
\def\SH#GetTokens#1,#2\#empty{%
\def\SH#token{#1}%
\ifx\SH#trimtokens\SH#true% strip spaces if requested
\TrimSpaces\SH#token%
\fi%
\SH#DefineCommand{\SH#FirstArgName}{\SH#token}%
\SH#DefineCommand{\SH#SecondArgName}{#2}%
}
\def\SH#CheckTokenSep#1,#2\#empty{%
\def\SH#CTSArgTwo{#2}%
\ifx\SH#CTSArgTwo\#empty%
\edef\SH#TokenValid{\SH#false}%
\else%
\edef\SH#TokenValid{\SH#true}%
\fi%
}
I will report this bugfix to the developer Sascha Herpers
Try the xstring package:
\usepackage{xstring}
[…]
\StrSubstitute{File,New}{,}{\(\rightarrow\)}
There's a LaTeX package called tokenizer which may help you to do what you want.
Here's a hack (but pure LaTeX, no internals) which gets close to what I think you want, but with some extraneous spaces I haven't quite been able to fix. Perhaps Will Robertson can advise further? Unlike his slightly more polished answer, I haven't parameterised the bits and pieces, Here goes:
\usepackage{forloop}
\usepackage[trim]{tokenizer}
...
\newcounter{rrCount}
\newcommand{\replace}[1]{%
\GetTokens{rrFirst}{rrRest}{#1,}%
\textbf{\rrFirst}%
\forloop{rrCount}{0}{\value{rrCount} < 100}{%
\ifthenelse{\equal{\rrRest}{}}{%
\setcounter{rrCount}{101}%
}{%
\GetTokens{rrFirst}{rrRest}{\rrRest}%
$\rightarrow$\textbf{\rrFirst}%
}%
}%
}%
% -----------------------------------------------------------------
\replace{a1}\\
\replace{a2,b2}\\
\replace{a3,b3,c3}\\
OK, I withdraw this answer. Thanks for clarifying the question.
I suspect this may not be what you want, but here goes anyway:
\newcommand{\replace}[2]{\textbf{#1}$\rightarrow$\textbf{#2}}
\replace{File}{New}
If this isn't what you're looking for, could you clarify the question, please?

Resources