Why does using ledpar cause a document to fail? - latex

Here is my minimal LaTeX document:
\documentclass{article}
\usepackage[polutonikogreek,english]{babel}
\newcommand{\Gk}[1]{\selectlanguage{polutonikogreek}#1\selectlanguage{english}}
\usepackage{ledmac}
\newcommand{\cn}[1]{\Afootnote{#1}}
\usepackage{ledpar}
\begin{document}
\beginnumbering
\pstart
\edtext{apostle}{\cn{\Gk{apostoloc}}}
\pend
\endnumbering
\end{document}
Executing latex test.tex produces the following error:
...
Section 1 (./test.1)
! Missing control sequence inserted.
<inserted text>
\inaccessible
l.15 \pend
?
Some notes:
The DVI produced looks fine despite the error.
Commenting out the \usepackage{ledpar} fixes the problem.
Not using the \Gk command also solves the problem. (But sort of defeats the purpose of having a footnote.)
What's going on here and how do I get around the error message?

According to the FAQ:
Sometimes LaTeX saves data it will reread later. These data are often the argument of some command; they are the so-called moving arguments. (‘Moving’ because data are moved around.) Candidates are all arguments that may go into table of contents, list of figures, etc.; namely, data that are written to an auxiliary file and read in later. Other places are those data that might appear in head- or footlines. Section headings and figure captions are the most prominent examples; there’s a complete list in Lamport’s book (see TeX-related books).
What’s going on really, behind the scenes? The commands in moving arguments are normally expanded to their internal structure during the process of saving. Sometimes this expansion results in invalid TeX code, which shows either during expansion or when the code is processed again. Protecting a command, using “\protect\cmd” tells LaTeX to save \cmd as \cmd, without expanding it at all.
So the \Gk command gets expanded too early in the process of TeXing the file and results in illegal code. The simplest solution is to declare the command robust:
\usepackage{makerobust}
\DeclareRobustCommand{\Gk}[1]{\selectlanguage{polutonikogreek}#1\selectlanguage{english}}
As to why using the ledpar package produces the error, I'm less certain. In order to facilitate notes in both the left and right side of parallel text, the ledpar package needs to redefine virtually every command provided by the ledmac package. Although I have not found the offending difference, one or more of the redefinitions must cause fragile commands to be expanded prematurely.

Related

How to define a multipage environment not interrupted by tables and figures?

I have defined a new LaTeX environment for excursions in a book chapter I am writing. The environment is multipage and often includes inline images. Moreover, I am using the shaded environment to give the environment a background colour to make it stand out a bit.
However, the environment, as shown below, is split up by floating tables and images, which makes the flow of the environment visually more difficult to follow. For example, it is now difficult to see if that floating image or table is part (the missing background colour does not help). So, I like to extend my environment to disallow it to be interrupted by floating elements, but do not know how to get that done.
\newcounter{bioclipse}
\def\thebioclipse{\thechapter-\arabic{bioclipse}}
\newenvironment{bioclipse}[2][]{\begin{small}\begin{shaded}\refstepcounter{bioclipse} \par\medskip\noindent%
\textbf{Bioclipse Excursion~\thebioclipse #1: #2
\vspace{0.1cm} \hrule \vspace{0.1cm}}
\rmfamily}{\medskip \end{shaded}\end{small}}
Any solution to disallow interruption is fine, even if the background colour is done differently.
The algorithm for insertion is rather complex. Basically you want that any pending insertion must not be put into the page where the env bioclipse apply. As first fast solution you could flush all the insertion first, and then start the new chapter. If you want to put figures or whatever into the environment, and you want them to be "flushed" only after the last page where the env is at work... second fast solution is: put them after the environment directly!! So they won't "annoy" the page/s at all (of course, avoid using of footnotes).
The other solution (making it someway automatic) is a little bit tricky. Insertion places for "pending" materials are chosen while constructing the vertical list that is the page ("candidate"), in the output routine. This means you have to play with the output routine at worst; but maybe it is too much, unless you're planning your own TeX format, and maybe LaTeX gives easier choices...
Digging a bit in LaTeX codes, I see there's a conditional you can try to use, it is \#insert* i.e. \#insertfalse and \#inserttrue. If you're lucky they "drive" the possibility of putting insertions, so that at the beginning of your env you can say \#insertfalse and at the end \#inserttrue. Try, I am not saying it works.
As maybe you know to use # as letter catcode so that it can be part of a "command" name, you have to use \makeatletter and \makeatother when you finished (likely default class/style preamble does it for you).
You could also be iterested in having a look at placeins style (it could be already in your installation, otherwise, see here ) which apparently could solve (part of) your problem.

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.

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.

Tex command which affects the next complete word

Is it possible to have a TeX command which will take the whole next word (or the next letters up to but not including the next punctuation symbol) as an argument and not only the next letter or {} group?
I’d like to have a \caps command on certain acronyms but don’t want to type curly brackets over and over.
First of all create your command, for example
\def\capsimpl#1{{\sc #1}}% Your main macro
The solution to catch a space or punctuation:
\catcode`\#=11
\def\addtopunct#1{\expandafter\let\csname punct#\meaning#1\endcsname\let}
\addtopunct{ }
\addtopunct{.} \addtopunct{,} \addtopunct{?}
\addtopunct{!} \addtopunct{;} \addtopunct{:}
\newtoks\capsarg
\def\caps{\capsarg{}\futurelet\punctlet\capsx}
\def\capsx{\expandafter\ifx\csname punct#\meaning\punctlet\endcsname\let
\expandafter\capsend
\else \expandafter\continuecaps\fi}
\def\capsend{\expandafter\capsimpl\expandafter{\the\capsarg}}
\def\continuecaps#1{\capsarg=\expandafter{\the\capsarg#1}\futurelet\punctlet\capsx}
\catcode`\#=12
#Debilski - I wrote something similar to your active * code for the acronyms in my thesis. I activated < and then \def<#1> to print the acronym, as well as the expansion if it's the first time it's encountered. I also went a bit off the deep end by allowing defining the expansions in-line and using the .aux files to send the expansions "back in time" if they're used before they're declared, or to report errors if an acronym is never declared.
Overall, it seemed like it would be a good idea at the time - I rarely needed < to be catcode 12 in my actual text (since all my macros were in a separate .sty file), and I made it behave in math mode, so I couldn't foresee any difficulties. But boy was it brittle... I don't know how many times I accidentally broke my build by changing something seemingly unrelated. So all that to say, be very careful activating characters that are even remotely commonly-used.
On the other hand, with XeTeX and higher unicode characters, it's probably a lot safer, and there are generally easy ways to type these extra characters, such as making a multi (or compose) key (I usually map either numlock or one of the windows keys to this), so that e.g. multi-!-! produces ¡). Or if you're running in emacs, you can use C-\ to switch into TeX input mode briefly to insert unicode by typing the TeX command for it (though this is a pain for actually typing TeX documents, since it intercepts your actual \'s, and please please don't try defining your own escape character!)
Regarding whitespace after commands: see package xspace, and TeX FAQ item Commands gobble following space.
Now why this is very difficult: as you noted yourself, things like that can only be done by changing catcodes, it seems. Catcodes are assigned to characters when TeX reads them, and TeX reads one line at a time, so you can not do anything with other spaces on the same line, IMHO. There might be a way around this, but I do not see it.
Dangerous code below!
This code will do what you want only at the end of the line, so if what you want is more "fluent" typing without brackets, but you are willing to hit 'return' after each acronym (and not run any auto-indent later), you can use this:
\def\caps{\begingroup\catcode`^^20 =11\mcaps}
\def\mcaps#1{\def\next##1 {\sc #1##1\catcode`^^20 =10\endgroup\ }\next}
One solution might be setting another character as active and using this one for escaping. This does not remove the need for a closing character but avoids typing the \caps macro, thus making it overall easier to type.
Therefore under very special circumstances, the following works.
\catcode`\*=\active
\def*#1*{\textsc{\MakeTextLowercase{#1}}}
Now follows an *Acronym*.
Unfortunately, this makes uses of \section*{} impossible without additional macro definitions.
In Xetex, it seems to be possible to exploit unicode characters for this, so one could define
\catcode`\•=\active
\def•#1•{\textsc{\MakeTextLowercase{#1}}}
Now follows an •Acronym•.
Which should reduce the effects on other commands but of course needs to have the character ‘•’ mapped to the keyboard somewhere to be of use.

How to colour lines in LaTeX that match regular expressions

I'm currently using the LaTeX listings package to display a block of code, and a diff of the file against a previous version.
Both blocks are coloured by listings as if they are code (which they are) but I would like to colour the diff similarly to emacs diff-mode - red for lines matching ^-, green for ^\+ etc.
Does anyone know if there is a package to achieve this, whether listings can do it, or whether it is possible to write a LaTeX command to do it? (or rather - where a good source of information on the latter can be found?)
Thanks,
Hud
If you just want unadorned diffs, Pygments supports them, so texments (a latex front-end for pygments) does what you want.
But I guess that what you want is to have diff's coloured, while having the syntax of the underlying code highlighted appropriately. This you can't do properly the usual way, in general, because syntax highlighting may depend on the state from the previous line, and with udiffs the previous line may be missing, or an inserted line might follow a deleted line, &c.
To do the right thing, you'd need to syntax highlight the old and new versions, and then scramble the highlighted versions together to get the right output. Quite a bit of work, and I've not heard of anyone who's done that.
You could also try simply modifying the usual syntax highlighter for a language, removing highlighting rules that involve multiline state, and inserting rules to colour lines with udiff markup. Cf. Pygments' Write your own lexer; what you want from diff is trickier, since you want what is coloured to be highlighted, so you can't just make the lines into GenericTokens; I don't know what the right way to do this is.
Use a scripting tool such as sed, awk, Python [lang of choice]to produce the Latex source code.

Resources