How do I reference a label in one latex file from another? - reference

I have two files, a.tex and b.tex. In a.tex I have a label, \label{stuff}. In b.tex I need to refer to this label, \ref{stuff}.
I also have a main.tex file. Regardless of if I use \include{a}\include{b} or use \input{a}\input{b} the reference is not included in the pdf generated from main.tex.
The document type of main.tex is tufte-book.
Ideas?
main.tex
% Tufte-Style Book (Documentation Template)
% LaTeX Template
% Version 1.0 (5/1/13)
%
% This template has been downloaded from:
% http://www.LaTeXTemplates.com
%
% Original author:
% The Tufte-LaTeX Developers (tufte-latex.googlecode.com)
%
% License:
% Apache License (Version 2.0)
%
% IMPORTANT NOTE:
% In addition to running BibTeX to compile the reference list from the .bib
% file, you will need to run MakeIndex to compile the index at the end of the
% document.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%----------------------------------------------------------------------------------------
% PACKAGES AND OTHER DOCUMENT CONFIGURATIONS
%----------------------------------------------------------------------------------------
\documentclass{tufte-book} % Use the tufte-book class which in turn uses the tufte-common class
\hypersetup{colorlinks} % Comment this line if you don't wish to have colored links
\usepackage{microtype} % Improves character and word spacing
\usepackage{lipsum} % Inserts dummy text
\usepackage{booktabs} % Better horizontal rules in tables
\usepackage{amsmath}
\usepackage{tikz,pgfplots}
\usepackage{graphicx} % Needed to insert images into the document
\graphicspath{{graphics/}} % Sets the default location of pictures
\setkeys{Gin}{width=\linewidth,totalheight=\textheight,keepaspectratio} % Improves figure scaling
\usepackage{fancyvrb} % Allows customization of verbatim environments
\fvset{fontsize=\normalsize} % The font size of all verbatim text can be changed here
\newcommand\when{\ when\ }
\newcommand\otherwise{\ otherwise\ }
\newcommand\doubleplus{+\kern-1.3ex+\kern0.8ex}
\newcommand\mdoubleplus{\ensuremath{\mathbin{+\mkern-10mu+}}}
\newcommand{\false}{f\kern-0.25ex{alse}}
\newcommand\mfalse{\ensuremath{\mathbin{f\mkern-5mu alse}}}
\newcommand\f{\textit{f}}
\newcommand{\hangp}[1]{\makebox[0pt][r]{(}#1\makebox[0pt][l]{)}} % New command to create parentheses around text in tables which take up no horizontal space - this improves column spacing
\newcommand{\hangstar}{\makebox[0pt][l]{*}} % New command to create asterisks in tables which take up no horizontal space - this improves column spacing
\usepackage{xspace} % Used for printing a trailing space better than using a tilde (~) using the \xspace command
\newcommand{\monthyear}{\ifcase\month\or January\or February\or March\or April\or May\or June\or July\or August\or September\or October\or November\or December\fi\space\number\year} % A command to print the current month and year
\newcommand{\openepigraph}[2]{ % This block sets up a command for printing an epigraph with 2 arguments - the quote and the author
\begin{fullwidth}
\sffamily\large
\begin{doublespace}
\noindent\allcaps{#1}\\ % The quote
\noindent\allcaps{#2} % The author
\end{doublespace}
\end{fullwidth}
}
\newcommand{\blankpage}{\newpage\hbox{}\thispagestyle{empty}\newpage} % Command to insert a blank page
\usepackage{units} % Used for printing standard units
\newcommand{\hlred}[1]{\textcolor{Maroon}{#1}} % Print text in maroon
\newcommand{\hangleft}[1]{\makebox[0pt][r]{#1}} % Used for printing commands in the index, moves the slash left so the command name aligns with the rest of the text in the index
\newcommand{\hairsp}{\hspace{1pt}} % Command to print a very short space
\newcommand{\ie}{\textit{i.\hairsp{}e.}\xspace} % Command to print i.e.
\newcommand{\eg}{\textit{e.\hairsp{}g.}\xspace} % Command to print e.g.
\newcommand{\na}{\quad--} % Used in tables for N/A cells
\newcommand{\measure}[3]{#1/#2$\times$\unit[#3]{pc}} % Typesets the font size, leading, and measure in the form of: 10/12x26 pc.
\newcommand{\tuftebs}{\symbol{'134}} % Command to print a backslash in tt type in OT1/T1
\providecommand{\XeLaTeX}{X\lower.5ex\hbox{\kern-0.15em\reflectbox{E}}\kern-0.1em\LaTeX}
\newcommand{\tXeLaTeX}{\XeLaTeX\index{XeLaTeX#\protect\XeLaTeX}} % Command to print the XeLaTeX logo while simultaneously adding the position to the index
\newcommand{\doccmdnoindex}[2][]{\texttt{\tuftebs#2}} % Command to print a command in texttt with a backslash of tt type without inserting the command into the index
\newcommand{\doccmddef}[2][]{\hlred{\texttt{\tuftebs#2}}\label{cmd:#2}\ifthenelse{\isempty{#1}} % Command to define a command in red and add it to the index
{ % If no package is specified, add the command to the index
\index{#2 command#\protect\hangleft{\texttt{\tuftebs}}\texttt{#2}}% Command name
}
{ % If a package is also specified as a second argument, add the command and package to the index
\index{#2 command#\protect\hangleft{\texttt{\tuftebs}}\texttt{#2} (\texttt{#1} package)}% Command name
\index{#1 package#\texttt{#1} package}\index{packages!#1#\texttt{#1}}% Package name
}}
\newcommand{\doccmd}[2][]{% Command to define a command and add it to the index
\texttt{\tuftebs#2}%
\ifthenelse{\isempty{#1}}% If no package is specified, add the command to the index
{%
\index{#2 command#\protect\hangleft{\texttt{\tuftebs}}\texttt{#2}}% Command name
}
{%
\index{#2 command#\protect\hangleft{\texttt{\tuftebs}}\texttt{#2} (\texttt{#1} package)}% Command name
\index{#1 package#\texttt{#1} package}\index{packages!#1#\texttt{#1}}% Package name
}}
% A bunch of new commands to print commands, arguments, environments, classes, etc within the text using the correct formatting
\newcommand{\docopt}[1]{\ensuremath{\langle}\textrm{\textit{#1}}\ensuremath{\rangle}}
\newcommand{\docarg}[1]{\textrm{\textit{#1}}}
\newenvironment{docspec}{\begin{quotation}\ttfamily\parskip0pt\parindent0pt\ignorespaces}{\end{quotation}}
\newcommand{\docenv}[1]{\texttt{#1}\index{#1 environment#\texttt{#1} environment}\index{environments!#1#\texttt{#1}}}
\newcommand{\docenvdef}[1]{\hlred{\texttt{#1}}\label{env:#1}\index{#1 environment#\texttt{#1} environment}\index{environments!#1#\texttt{#1}}}
\newcommand{\docpkg}[1]{\texttt{#1}\index{#1 package#\texttt{#1} package}\index{packages!#1#\texttt{#1}}}
\newcommand{\doccls}[1]{\texttt{#1}}
\newcommand{\docclsopt}[1]{\texttt{#1}\index{#1 class option#\texttt{#1} class option}\index{class options!#1#\texttt{#1}}}
\newcommand{\docclsoptdef}[1]{\hlred{\texttt{#1}}\label{clsopt:#1}\index{#1 class option#\texttt{#1} class option}\index{class options!#1#\texttt{#1}}}
\newcommand{\docmsg}[2]{\bigskip\begin{fullwidth}\noindent\ttfamily#1\end{fullwidth}\medskip\par\noindent#2}
\newcommand{\docfilehook}[2]{\texttt{#1}\index{file hooks!#2}\index{#1#\texttt{#1}}}
\newcommand{\doccounter}[1]{\texttt{#1}\index{#1 counter#\texttt{#1} counter}}
\usepackage{makeidx} % Used to generate the index
\makeindex % Generate the index which is printed at the end of the document
% This block contains a number of shortcuts used throughout the book
\newcommand{\vdqi}{\textit{VDQI}\xspace}
\newcommand{\ei}{\textit{EI}\xspace}
\newcommand{\ve}{\textit{VE}\xspace}
\newcommand{\be}{\textit{BE}\xspace}
\newcommand{\VDQI}{\textit{The Visual Display of Quantitative Information}\xspace}
\newcommand{\EI}{\textit{Envisioning Information}\xspace}
\newcommand{\VE}{\textit{Visual Explanations}\xspace}
\newcommand{\BE}{\textit{Beautiful Evidence}\xspace}
\newcommand{\TL}{Tufte-\LaTeX\xspace}
%----------------------------------------------------------------------------------------
% BOOK META-INFORMATION
%----------------------------------------------------------------------------------------
\title{A Study in Algorithms} % Title of the book
\author[Lee Barney]{Lee Barney} % Author
%\publisher{Lee S. Barney} % Publisher
%----------------------------------------------------------------------------------------
\begin{document}
\frontmatter
\maketitle % Print the title page
%----------------------------------------------------------------------------------------
% COPYRIGHT PAGE
%----------------------------------------------------------------------------------------
\newpage
\begin{fullwidth}
~\vfill
\thispagestyle{empty}
\setlength{\parindent}{0pt}
\setlength{\parskip}{\baselineskip}
Copyright \copyright\ \the\year\ \thanklessauthor
%\par\smallcaps{Published by \thanklesspublisher}
%\par\smallcaps{tufte-latex.googlecode.com}
\par All rights reserved. \index{license}
\par\textit{First printing, \monthyear}
\end{fullwidth}
%----------------------------------------------------------------------------------------
\tableofcontents % Print the table of contents
%----------------------------------------------------------------------------------------
%\listoffigures % Print a list of figures
%----------------------------------------------------------------------------------------
%\listoftables % Print a list of tables
%----------------------------------------------------------------------------------------
% INTRODUCTION
%----------------------------------------------------------------------------------------
\cleardoublepage
\include{intro}
%----------------------------------------------------------------------------------------
\mainmatter
\include{a}
\include{b}
%----------------------------------------------------------------------------------------
\backmatter
%----------------------------------------------------------------------------------------
% BIBLIOGRAPHY
%----------------------------------------------------------------------------------------
\bibliography{algorithms} % Use the bibliography.bib file for the bibliography
\bibliographystyle{plainnat} % Use the plainnat style of referencing
%----------------------------------------------------------------------------------------
\printindex % Print the index at the very end of the document
\end{document}```

I am using similar thing in my thesis. Using \hyperref[label]{text you wanna show} in the text is working for me. Don't forget to have package hyperref included.
\label{ab:PCL} % this is inside my abbreviations file
---
\hyperref[ab:PCL]{polycaprolactone} % this is in my text file

Related

Minimal self-compiling to .pdf Rmarkdown file

I need to compose a simple rmarkdown file, with text, code and the results of executed code included in a resulting PDF file. I would prefer if the source file is executable and self sifficient, voiding the need for a makefile.
This is the best I have been able to achieve, and it is far from good:
#!/usr/bin/env Rscript
library(knitr)
pandoc('hw_ch4.rmd', format='latex')
# TODO: how to NOT print the above commands to the resulting .pdf?
# TODO: how to avoid putting everyting from here on in ""s?
# TODO: how to avoid mentioning the file name above?
# TODO: how to render special symbols, such as tilde, miu, sigma?
# Unicode character (U+3BC) not set up for use with LaTeX.
# See the inputenc package documentation for explanation.
# nano hw_ch4.rmd && ./hw_ch4.rmd && evince hw_ch4.pdf
"
4E1. In the model definition below, which line is the likelihood?
A: y_i is the likelihood, based on the expectation and deviation.
4M1. For the model definition below, simulate observed heights from the prior (not the posterior).
A:
```{r}
points <- 10
rnorm(points, mean=rnorm(points, 0, 10), sd=runif(points, 0, 10))
```
4M3. Translate the map model formula below into a mathematical model definition.
A:
```{r}
flist <- alist(
y tilda dnorm( mu , sigma ),
miu tilda dnorm( 0 , 10 ),
sigma tilda dunif( 0 , 10 )
)
```
"
Result:
What I eventually came to use is the following header. At first it sounded neat, but later I realized
+ is indeed easy to compile in one step
- this is code duplication
- mixing executable script and presentation data in one file is a security risk.
Code:
#!/usr/bin/env Rscript
#<!---
library(rmarkdown)
argv <- commandArgs(trailingOnly=FALSE)
fname <- sub("--file=", "", argv[grep("--file=", argv)])
render(fname, output_format="pdf_document")
quit(status=0)
#-->
---
title:
author:
date: "compiled on: `r Sys.time()`"
---
The quit() line is supposed to guarantee that the rest of the file is treated as data. The <!--- and --> comments are to render the executable code as comments in the data interpretation. They are, in turn, hidden by the #s from the shell.

Why is python pulling in symbols instead of text from a pdf

I am trying to loop through a set of pdfs (all are OCR'd) in a set of folders and search for key terms in the pdf and if pdf contains a certain term, then save the folder name, file name, etc.. This code is working to an extent. Except, it is missing a few pdfs within the search terms. The reason is because when I read in a couple of the pdfs it displays some jibberish (to me at least) on a couple of pages. For example, say I have read in a pdf named 'the_one.pdf'. It has 278 pages. When I go into adobe acrobat to search this document, I can find 'Search Term 1' on page 171, but when it is read with python, python outputs something like this:
-ˆ˜
%
˜%˝ˆ
,˙
˚
%.
%,˛#
%˜˚
0"
˚˝
%
˚˝ˆ˙)˛˚˜
˚0˛˚
:&;
#˛˘˘˙
˚%˚
"
%˚˛˘
ˆ
˛˚,˚
"
$%˚˚%
%
˝%.
"˛
"
%˜
˝,
-ˆ
%˘˙
˛˘˚
0"
"
˛
.˛˝
%˜˚
˝˜
.%
!˝ˆ%
4
0"
"
%˜˚
˛
%˛˘˘˙
!˝ˆ˜
%
˛ ˚˝ˆ˙)˛˚˜
˚0˛
!˝ˆ%
.˛˝˘˙8
˛˜
%
0"
"
˚
˛ #%˛%
"˛
˚ˆ˘˚
˛ ˛˚˛˝%
0"%ˆ
˛˙
!˝ˆ˛˘
%˜
%
%"
˚ˆ˝%
#
7
˘˛˘˙
:&;
˛˘˚%
˛˚,˚
"
$%˚˚%
%
˝%.
%
%˜
˝,
6
;˚
%˜
˛%
"
$%˚˚%
˚"%ˆ˘˜
˘˝˘˙
%
"˛
.˝˚
%
˚˛˜)˛˘%
/ˇ˚
˘˝˘˙
˝˘ˆ˜
˚˛˜)˛˘%
/ˇ˚
"˛
˛
#˚˜
˛˚
9$
˜˛˚
˜˛˘˚
:
"˚
˘
.˝˚
%
˚˛˜)˛˘%
/ˇ˚
˛
˜˜
%
˛˘˙
%
9$
˜˛˚
˜˛˘˚
"˛
˛
˜ˆ˛˘˘˙
#˚˜
˛˚
/ˇ˚
4˛˜
˚ˆ˝"
˚
˛
˛˘˚%
˛%˜
%
ˆ˚
˛˘
%˜˘˚8
7
9"˚
#%˛%˚
%.
˛,
˘˛˝
%
"
˘"%
ˆ
˝˛
˛˘˚%
˛,
ˆ˚
%.
˘˝%˝
˚˙˚˚
%
˚˝ˆ˙)˛˚˜
˚0˛
!˝ˆ%
.˛˝˘˚
&%
!˛˘
˛ ˛,
˛˝˛
˛˙
˚
%
%
%
%
/ˇ˚
˛ -ˆ˚
.%
-ˆ%˛%
4<
˝6
=8
.%
˛ ˚˝.˝
˚˝ˆ˙)˛˚˜
˚0˛
˛˜
˝
˛˝,
Of course, it displays the majority of pages correctly, but for some reason it won't display a couple of them. For confidentiality reasons, I can't post the pdfs. Does anyone have any idea why this is happening?
Also, anything you can point out to speed up my code or make it more dynamic is helpful as well. Always looking to learn.
Best,
J.Dykstra
import PyPDF2
from os import walk
import os
import re
import csv
pdf_location = r'PDF Directory'
x = ['Search term 1', 'Search term 2', 'Search term 3', 'etc..']
key_terms = []
rule = []
filenamey = []
for dirpath, dirnames, filenames in walk(pdf_location):
for filename in filenames:
if filename.endswith('.pdf'):
pdfFileObj = open(os.path.join(dirpath,filename), 'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFileObj, strict = False)
num_pages = pdfReader.numPages
count = 0
text = ""
while count < num_pages:
pageObj = pdfReader.getPage(count)
count +=1
text += pageObj.extractText()
for i in x:
if re.search(i,text, re.IGNORECASE):
rulex = dirpath.split("Rule")[1]
filenamex = filename
key_termx = x[0]
key_terms.append(key_termx)
rule.append(rulex)
Parsing PDF is a complex task, the 1.7 spec has around 750 pages and Adobe makes money with it - thats why it works for them.
PDFs internally have tables that hold
"how letters look" (glyphs)
"what unicode letters those glyphs are mapped to" (you need that to copy&paste someting from pdf correctly)
and a cross-ref which glyph mapps to what unicode. Fonts might be (partly) be embedded in the pdf as well.
Thats (one reason) why pdfs can look 100% ok, could be "OCR"ed ok - but if you just copy&paste from a document that has a corrupt mapping between glyphs and unicode points, you only get gibberish.
I have heard some programms even provide unicode mappings for all glyphs but they do not match up at all ... on purpose (or bad quality) - to prevent copy&paste.
Bottom line: you can try to re-OCR some pages, you could use Adobe Acrobat PRO to extract text from PDF (it has build in ocr features) that give you gibberish or just skip it.
You can try some other pdf-reading framework, maybe they got something not quite right - but chances are slim if it almost always works but just not for a few special pdfs.
I am just a novice in pdf - there are some more advanced ppl around to pipe in on this - but if you cannot share the pdf its going to be hard to advice anything.
Alternate approaches: Searching text in a PDF using Python?

lhs2Tex produces invalid .tex file

I ran lhs2Tex on my f.lhs file ($ lhs2Tex f.lhs > f.tex). It completed successfully, creating f.tex and producing no errors.
However, when I then run $ pdflatex f.tex, I get (some output followed by) the following error:
Runaway argument?
\ignorespaces \SaveRestoreHook \column {B}{#{}>{\hspre }l<{\hspost }#\ETC.
! File ended while scanning use of \PT#scantoend.
<inserted text>
\par
<*> core.tex
?
Why is the output of lhs2Tex failing to compile to valid tex? It seems that lhs2Tex should not generate invalid tex, even with invalid input.
I am not sure of the best way to debug this; the error message that I have points to f.tex, but that is a long generated output, not my written code. In my code (f.lhs), I am not sure where to start since lhs2Tex sends it to f.tex with no errors or output at all.
Any suggestions on how to approach debugging this?
Edit:
I removed a bunch of the generated code while checking to ensure that what remained still caused the same error.
Here is what remains; this minimized version of f.tex causes the same error.
\documentclass{article}
%% ODER: format == = "\mathrel{==}"
%% ODER: format /= = "\neq "
%
%
\makeatletter
\#ifundefined{lhs2tex.lhs2tex.sty.read}%
{\#namedef{lhs2tex.lhs2tex.sty.read}{}%
\newcommand\SkipToFmtEnd{}%
\newcommand\EndFmtInput{}%
\long\def\SkipToFmtEnd#1\EndFmtInput{}%
}\SkipToFmtEnd
\newcommand\ReadOnlyOnce[1]{\#ifundefined{#1}{\#namedef{#1}{}}\SkipToFmtEnd}
\usepackage{amstext}
\usepackage{amssymb}
\usepackage{stmaryrd}
\newcommand{\tex}[1]{\text{\texfamily#1}} % NEU
\newcommand{\Sp}{\hskip.33334em\relax}
\usepackage{polytable}
%mathindent has to be defined
\#ifundefined{mathindent}%
{\newdimen\mathindent\mathindent\leftmargini}%
{}%
\def\resethooks{%
\global\let\SaveRestoreHook\empty
\global\let\ColumnHook\empty}
\newcommand*{\savecolumns}[1][default]%
{\g#addto#macro\SaveRestoreHook{\savecolumns[#1]}}
\newcommand*{\restorecolumns}[1][default]%
{\g#addto#macro\SaveRestoreHook{\restorecolumns[#1]}}
\newcommand*{\aligncolumn}[2]%
{\g#addto#macro\ColumnHook{\column{#1}{#2}}}
\resethooks
\newcommand{\visiblecomments}{%
\let\onelinecomment=\onelinecommentchars
\let\commentbegin=\commentbeginchars
\let\commentend=\commentendchars}
\visiblecomments
\newlength{\blanklineskip}
\setlength{\blanklineskip}{0.66084ex}
\EndFmtInput
\makeatother
%
%
%
%
%
%
% This package provides two environments suitable to take the place
% of hscode, called "plainhscode" and "arrayhscode".
%
% The plain environment surrounds each code block by vertical space,
% and it uses \abovedisplayskip and \belowdisplayskip to get spacing
% similar to formulas. Note that if these dimensions are changed,
% the spacing around displayed math formulas changes as well.
% All code is indented using \leftskip.
%
% Changed 19.08.2004 to reflect changes in colorcode. Should work with
% CodeGroup.sty.
%
\ReadOnlyOnce{polycode.fmt}%
\makeatletter
\newcommand{\hsnewpar}[1]%
{{\parskip=0pt\parindent=0pt\par\vskip #1\noindent}}
% can be used, for instance, to redefine the code size, by setting the
% command to \small or something alike
\newcommand{\hscodestyle}{}
% The command \sethscode can be used to switch the code formatting
% behaviour by mapping the hscode environment in the subst directive
% to a new LaTeX environment.
\newcommand{\sethscode}[1]%
{\expandafter\let\expandafter\hscode\csname #1\endcsname
\expandafter\let\expandafter\endhscode\csname end#1\endcsname}
% "plain" mode is the proposed default.
% It should now work with \centering.
% This required some changes. The old version
% is still available for reference as oldplainhscode.
\newenvironment{plainhscode}%
{\hsnewpar\abovedisplayskip
\advance\leftskip\mathindent
\hscodestyle
\let\hspre\(\let\hspost\)%
\pboxed}%
{\endpboxed%
\hsnewpar\belowdisplayskip
\ignorespacesafterend}
% Here, we make plainhscode the default environment.
\newcommand{\plainhs}{\sethscode{plainhscode}}
\newcommand{\oldplainhs}{\sethscode{oldplainhscode}}
\plainhs
%
\begin{document}
\begin{hscode}\SaveRestoreHook
\ColumnHook
\end{hscode}\resethooks
\end{document}
\end{document}

Ignore the comment sign (%) in m-file within a string

In my code I have the following line:
fprintf(logfile,'Parameters: Size: %d\tH: %.4f\tF: %.1f\tI: %.3f\tR: %d\tSigma: %d\tDisp: %.1f\r\n',parameter_sets(ps,:));
which is too long, so I want to break it to:
fprintf(logfile,'Parameters: Size: %d\tH: %.4f\tF: %.1f\tI: %.3f\tR: ...
%d\tSigma: %d\tDisp: %.1f\r\n',parameter_sets(ps,:));
However, since the brake is within a string, MATLAB see the formatting %d sign in the second line as a start of a comment, and ignore this line (and produce an error...).
So I tried to make it clearer with a [] that warp the string:
fprintf(logfile,['Parameters: Size: %d\tH: %.4f\tF: %.1f\tI: %.3f\tR: ...
%d\tSigma: %d\tDisp: %.1f\r\n'],parameter_sets(ps,:));
but no help, it still interpret the second line as a comment. I also tried with and without the ellipsis (...) in different places, with no success.
So how can I write a line in a formatted way (i.e. a reasonable length) if it has a % sign in it?
Divide it in two lines like this:
fprintf(logfile,['Parameters: Size: %d\tH: %.4f\tF: %.1f\tI: %.3f\tR:', ...
'%d\tSigma: %d\tDisp: %.1f\r\n'],parameter_sets(ps,:));
% notice the apostrophe and comma(',) before ellpsis(...) at the end of first line
% and apostrophe(') at the start of the second line

Pdflatex error when using {-" ... "-} inline TeX comments in lhs2TeX

I have the following code block in my .lhs file which uses inline TeX comments:
\begin{code}
main = print 0
{-"$\langle$Link$\rangle$"-}
\end{code}
However, after compiling with lhs2TeX, I get the following errors when compiling the generated .tex file:
! Missing $ inserted.
<inserted text>
$
l.269 \end{hscode}
\resethooks
I've inserted a begin-math/end-math symbol since I think
you left one out. Proceed, with fingers crossed.
! LaTeX Error: Bad math environment delimiter.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.269 \end{hscode}
\resethooks
Your command was ignored.
Type I <command> <return> to replace it with another command,
or <return> to continue without it.
! Missing $ inserted.
<inserted text>
$
l.269 \end{hscode}
\resethooks
I've inserted a begin-math/end-math symbol since I think
you left one out. Proceed, with fingers crossed.
! LaTeX Error: Bad math environment delimiter.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.269 \end{hscode}
\resethooks
Your command was ignored.
Type I <command> <return> to replace it with another command,
or <return> to continue without it.
! Missing $ inserted.
<inserted text>
$
l.269 \end{hscode}
\resethooks
I've inserted a begin-math/end-math symbol since I think
you left one out. Proceed, with fingers crossed.
! LaTeX Error: Bad math environment delimiter.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.269 \end{hscode}
\resethooks
Your command was ignored.
When I remove the " marks in the inline comment, the error disappears. Anyone know what's wrong?
P.S Here's the .tex file that lhs2TeX generates:
\documentclass{article}%% ODER: format == = "\mathrel{==}"
%% ODER: format /= = "\neq "
%
%
\makeatletter
\#ifundefined{lhs2tex.lhs2tex.sty.read}%
{\#namedef{lhs2tex.lhs2tex.sty.read}{}%
\newcommand\SkipToFmtEnd{}%
\newcommand\EndFmtInput{}%
\long\def\SkipToFmtEnd#1\EndFmtInput{}%
}\SkipToFmtEnd
\newcommand\ReadOnlyOnce[1]{\#ifundefined{#1}{\#namedef{#1}{}}\SkipToFmtEnd}
\usepackage{amstext}
\usepackage{amssymb}
\usepackage{stmaryrd}
\DeclareFontFamily{OT1}{cmtex}{}
\DeclareFontShape{OT1}{cmtex}{m}{n}
{<5><6><7><8>cmtex8
<9>cmtex9
<10><10.95><12><14.4><17.28><20.74><24.88>cmtex10}{}
\DeclareFontShape{OT1}{cmtex}{m}{it}
{<-> ssub * cmtt/m/it}{}
\newcommand{\texfamily}{\fontfamily{cmtex}\selectfont}
\DeclareFontShape{OT1}{cmtt}{bx}{n}
{<5><6><7><8>cmtt8
<9>cmbtt9
<10><10.95><12><14.4><17.28><20.74><24.88>cmbtt10}{}
\DeclareFontShape{OT1}{cmtex}{bx}{n}
{<-> ssub * cmtt/bx/n}{}
\newcommand{\tex}[1]{\text{\texfamily#1}} % NEU
\newcommand{\Sp}{\hskip.33334em\relax}
\newcommand{\Conid}[1]{\mathit{#1}}
\newcommand{\Varid}[1]{\mathit{#1}}
\newcommand{\anonymous}{\kern0.06em \vbox{\hrule\#width.5em}}
\newcommand{\plus}{\mathbin{+\!\!\!+}}
\newcommand{\bind}{\mathbin{>\!\!\!>\mkern-6.7mu=}}
\newcommand{\rbind}{\mathbin{=\mkern-6.7mu<\!\!\!<}}% suggested by Neil Mitchell
\newcommand{\sequ}{\mathbin{>\!\!\!>}}
\renewcommand{\leq}{\leqslant}
\renewcommand{\geq}{\geqslant}
\usepackage{polytable}
%mathindent has to be defined
\#ifundefined{mathindent}%
{\newdimen\mathindent\mathindent\leftmargini}%
{}%
\def\resethooks{%
\global\let\SaveRestoreHook\empty
\global\let\ColumnHook\empty}
\newcommand*{\savecolumns}[1][default]%
{\g#addto#macro\SaveRestoreHook{\savecolumns[#1]}}
\newcommand*{\restorecolumns}[1][default]%
{\g#addto#macro\SaveRestoreHook{\restorecolumns[#1]}}
\newcommand*{\aligncolumn}[2]%
{\g#addto#macro\ColumnHook{\column{#1}{#2}}}
\resethooks
\newcommand{\onelinecommentchars}{\quad-{}- }
\newcommand{\commentbeginchars}{\enskip\{-}
\newcommand{\commentendchars}{-\}\enskip}
\newcommand{\visiblecomments}{%
\let\onelinecomment=\onelinecommentchars
\let\commentbegin=\commentbeginchars
\let\commentend=\commentendchars}
\newcommand{\invisiblecomments}{%
\let\onelinecomment=\empty
\let\commentbegin=\empty
\let\commentend=\empty}
\visiblecomments
\newlength{\blanklineskip}
\setlength{\blanklineskip}{0.66084ex}
\newcommand{\hsindent}[1]{\quad}% default is fixed indentation
\let\hspre\empty
\let\hspost\empty
\newcommand{\NB}{\textbf{NB}}
\newcommand{\Todo}[1]{$\langle$\textbf{To do:}~#1$\rangle$}
\EndFmtInput
\makeatother
%
%
%
%
%
%
% This package provides two environments suitable to take the place
% of hscode, called "plainhscode" and "arrayhscode".
%
% The plain environment surrounds each code block by vertical space,
% and it uses \abovedisplayskip and \belowdisplayskip to get spacing
% similar to formulas. Note that if these dimensions are changed,
% the spacing around displayed math formulas changes as well.
% All code is indented using \leftskip.
%
% Changed 19.08.2004 to reflect changes in colorcode. Should work with
% CodeGroup.sty.
%
\ReadOnlyOnce{polycode.fmt}%
\makeatletter
\newcommand{\hsnewpar}[1]%
{{\parskip=0pt\parindent=0pt\par\vskip #1\noindent}}
% can be used, for instance, to redefine the code size, by setting the
% command to \small or something alike
\newcommand{\hscodestyle}{}
% The command \sethscode can be used to switch the code formatting
% behaviour by mapping the hscode environment in the subst directive
% to a new LaTeX environment.
\newcommand{\sethscode}[1]%
{\expandafter\let\expandafter\hscode\csname #1\endcsname
\expandafter\let\expandafter\endhscode\csname end#1\endcsname}
% "compatibility" mode restores the non-polycode.fmt layout.
\newenvironment{compathscode}%
{\par\noindent
\advance\leftskip\mathindent
\hscodestyle
\let\\=\#normalcr
\let\hspre\(\let\hspost\)%
\pboxed}%
{\endpboxed\)%
\par\noindent
\ignorespacesafterend}
\newcommand{\compaths}{\sethscode{compathscode}}
% "plain" mode is the proposed default.
% It should now work with \centering.
% This required some changes. The old version
% is still available for reference as oldplainhscode.
\newenvironment{plainhscode}%
{\hsnewpar\abovedisplayskip
\advance\leftskip\mathindent
\hscodestyle
\let\hspre\(\let\hspost\)%
\pboxed}%
{\endpboxed%
\hsnewpar\belowdisplayskip
\ignorespacesafterend}
\newenvironment{oldplainhscode}%
{\hsnewpar\abovedisplayskip
\advance\leftskip\mathindent
\hscodestyle
\let\\=\#normalcr
\(\pboxed}%
{\endpboxed\)%
\hsnewpar\belowdisplayskip
\ignorespacesafterend}
% Here, we make plainhscode the default environment.
\newcommand{\plainhs}{\sethscode{plainhscode}}
\newcommand{\oldplainhs}{\sethscode{oldplainhscode}}
\plainhs
% The arrayhscode is like plain, but makes use of polytable's
% parray environment which disallows page breaks in code blocks.
\newenvironment{arrayhscode}%
{\hsnewpar\abovedisplayskip
\advance\leftskip\mathindent
\hscodestyle
\let\\=\#normalcr
\(\parray}%
{\endparray\)%
\hsnewpar\belowdisplayskip
\ignorespacesafterend}
\newcommand{\arrayhs}{\sethscode{arrayhscode}}
% The mathhscode environment also makes use of polytable's parray
% environment. It is supposed to be used only inside math mode
% (I used it to typeset the type rules in my thesis).
\newenvironment{mathhscode}%
{\parray}{\endparray}
\newcommand{\mathhs}{\sethscode{mathhscode}}
% texths is similar to mathhs, but works in text mode.
\newenvironment{texthscode}%
{\(\parray}{\endparray\)}
\newcommand{\texths}{\sethscode{texthscode}}
% The framed environment places code in a framed box.
\def\codeframewidth{\arrayrulewidth}
\RequirePackage{calc}
\newenvironment{framedhscode}%
{\parskip=\abovedisplayskip\par\noindent
\hscodestyle
\arrayrulewidth=\codeframewidth
\tabular{#{}|p{\linewidth-2\arraycolsep-2\arrayrulewidth-2pt}|#{}}%
\hline\framedhslinecorrect\\{-1.5ex}%
\let\endoflinesave=\\
\let\\=\#normalcr
\(\pboxed}%
{\endpboxed\)%
\framedhslinecorrect\endoflinesave{.5ex}\hline
\endtabular
\parskip=\belowdisplayskip\par\noindent
\ignorespacesafterend}
\newcommand{\framedhslinecorrect}[2]%
{#1[#2]}
\newcommand{\framedhs}{\sethscode{framedhscode}}
% The inlinehscode environment is an experimental environment
% that can be used to typeset displayed code inline.
\newenvironment{inlinehscode}%
{\(\def\column##1##2{}%
\let\>\undefined\let\<\undefined\let\\\undefined
\newcommand\>[1][]{}\newcommand\<[1][]{}\newcommand\\[1][]{}%
\def\fromto##1##2##3{##3}%
\def\nextline{}}{\) }%
\newcommand{\inlinehs}{\sethscode{inlinehscode}}
% The joincode environment is a separate environment that
% can be used to surround and thereby connect multiple code
% blocks.
\newenvironment{joincode}%
{\let\orighscode=\hscode
\let\origendhscode=\endhscode
\def\endhscode{\def\hscode{\endgroup\def\#currenvir{hscode}\\}\begingroup}
%\let\SaveRestoreHook=\empty
%\let\ColumnHook=\empty
%\let\resethooks=\empty
\orighscode\def\hscode{\endgroup\def\#currenvir{hscode}}}%
{\origendhscode
\global\let\hscode=\orighscode
\global\let\endhscode=\origendhscode}%
\makeatother
\EndFmtInput
%
\begin{document}\section{}Precis 0
\newline{}\subsubsection*{\texttt{test0.tweave:}}\begin{hscode}\SaveRestoreHook
\column{B}{#{}>{\hspre}l<{\hspost}#{}}%
\column{E}{#{}>{\hspre}l<{\hspost}#{}}%
\>[B]{}\Varid{main}\mathrel{=}\Varid{print}\;\mathrm{0}{}\<[E]%
\\
\>[B]{}$\langle$Link$\rangle${}\<[E]%
\ColumnHook
\end{hscode}\resethooks
\section{}Precis 1
\newline{}\subsubsection*{\texttt{test0.tweave:}}\begin{hscode}\SaveRestoreHook
\column{B}{#{}>{\hspre}l<{\hspost}#{}}%
\column{E}{#{}>{\hspre}l<{\hspost}#{}}%
\>[B]{}\Varid{accum}\;\Varid{i}\mathrel{=}\Varid{id}{}\<[E]%
\ColumnHook
\end{hscode}\resethooks
\end{document}
The {-" ... "-} construct is quite low-level. It drops you in the environment TeX currently is in, which happens to be math mode already. So the solution to your problem is simple. Write the code to be inserted as if you are in math mode.
A different option is to use a normal comment, but make the comment characters invisible using \invisiblecomments. Normal comments are typeset as text by lhs2TeX.
The following complete lhs2TeX document demonstrates both options:
\documentclass{article}
%include polycode.fmt
\begin{document}
% Assume you are in math mode already:
\begin{code}
main = print 0
{-"\langle\text{Link}\rangle"-}
\end{code}
% This works, too:
\invisiblecomments
\begin{code}
main = print 0
{- $\langle$Link$\rangle$ -}
\end{code}
\end{document}

Resources