How to include both Agda and Isabelle code in the same latex file? - latex

I am creating presentation slides using beamer, and on the slides I want to include pieces of code from Agda and Isabelle standard libraries. All I can find online is generating latex from Agda (lagda) or from Isabelle (document preparation). I want to go the other way, as my slides will have code from different systems.
I can still use lstlisting or verbatim, but I would rather not copy-paste and reformat code.
I would prefer to have something like including line numbers from files, or maybe code between tags

Your best bet is to use the catchfilebetweentags package: given two files IsabelleCode.tex and AgdaCode.tex generated by the respective LaTeX backends of each language, you can capture the code between an opening tag %<*TAGNAME> and a closing tag %</TAGNAME> in either file by using the appropriate directive e.g.:
\ExecuteMetaData[IsabelleCode.tex]{TAGNAME}
\ExecuteMetaData[AgdaCode.tex]{TAGNAME}

With \lstinputlisting from the listings package, you can directly include code from the source file. Optionally you can specify the start and end line numbers
\documentclass{beamer}
\usepackage{listings}
\lstset{basicstyle=\ttfamily}
\begin{document}
\begin{frame}[fragile]
\lstinputlisting[firstline=1,lastline=7,language=C]{duck.C}
\end{frame}
\begin{frame}[fragile]
\lstinputlisting[firstline=1,lastline=7]{test.agada}
\end{frame}
\begin{frame}[fragile]
\lstinputlisting[firstline=1,lastline=7]{test.isabelle}
\end{frame}
\end{document}

At least for Agda, you can render individual .lagda modules into LaTeX. If you can do the same with Isabelle's pretty-printer, you should be then able to write toplevel LaTeX by hand, and then import (parts of) the Agda and Isabelle-generated LaTeX where appropriate.

Related

Extra brackets when showing reference with natbib in beamer

I am using natbib for bibliography in beamer using TeXstudio editor. Everything is fine except the extra unwanted brackets showing at the beginning of the reference by \bibliography{file name} placed in a separate frame. How I can get rid of such extra brackets? Thanks a lot. Here is a piece of my latex file:
\usepackage[sort&compress]{natbib}
\begin{document}
\begin{frame}{References}
\bibliographystyle{plain}
\bibliography{references}
\end{frame}
\end{document}
I used \cite when calling the reference in a frame. Here is the result in PDF file:
1. [ 1.]Maasoud Kaviany. Principles of heat transfer in porous media. Springer Science & Business Media, 2012.

How to include a LaTeX paper into another LaTeX paper in OverLeaf?

I have LaTeX paper that I need to include in my dissertation. I am working in OverLeaf. That paper was written under one document class while the university has its own class. I need to include the paper in the dissertation so that it has the university style, its images need to be listed in the list of figures of the dissertation, and the authors listed in the paper's bibliography needs to be listed in the university's class bibliography. Is there any way I can do that in OverLeaf?
There are several dedicated packages for this. For example have a look at the combine, subdocs or docmute packages (A list with even more suggestions can be fond at https://www.ctan.org/recommendations/docmute).
Here a short example with the docmute package
\documentclass{book}
\usepackage{lipsum}
\usepackage{docmute}
\begin{document}
\tableofcontents
text
\chapter{imported paper}
\input{test}% assuming your paper is called test.tex
\end{document}
A Latex document cannot have multiple \documentclass. One solution would be to split the header/content of your latex document in overleaf:
Create a master.tex with the documentclass and put all your content (text between \begin{document} and \end{document} in a second content.tex. In the master, just \input{content}.
In your dissertation, just copy content.tex, its figure and add \input{} in the master file of your University which has the specific documentclass and bibliography settings.

LaTeX: Search keywords in file

Can you use LaTeX to read in a file and search for key words? How would you do this?
I currently have code that reads a file and counts the number of lines. I was thinking that during each line read I could parse the line for specific words or tags. Can this be done?
\documentclass{book}
\usepackage{ifthen}
\newcounter{FileLines}
\newboolean{RestFile}
\newcommand{\FileLine}{}
\newread\File
\newcommand{\CountLinesInFile}[2]
{
\setboolean{RestFile}{true}
\setcounter{FileLines}{0}
\openin\File=#1
\whiledo{\boolean{RestFile}}
{
\ReadNextLine{\File}
\ifthenelse{\boolean{RestFile}}{
\stepcounter{FileLines}
}{}
}
\closein\File
}
\newcommand{\ReadNextLine}[1]{
\ifthenelse{\boolean{RestFile}}{
\read#1 to \FileLine
\ifeof#1\setboolean{RestFile}{false}
\else % if last line already is read, EOF appears here
\fi
}{}
}
\begin{document}
\CountLinesInFile{textfile.txt}
Lines in file: \arabic{FileLines}
\end{document}
It could probably be done, but I wouldn't recommend it. TeX has a lot of constraints and idiosyncracies that make it suboptimal for general-purpose programming.
Using a general-purpose programming language like Python, Ruby, Perl or Lua would make the task a lot easier.
I'm specifically including Lua here because it was chosen as the embedded scripting language for luaTeX, the next generation of pdfTeX. With luaTex it is possible to embed Lua code in your TeX documents. It also exposes the internals of the TeX engine to Lua, which opens a whole world of possibilities for package writers.

Extracting code from beamer presentation?

Some years ago I created a Beamer presentation (using only basic features). Unfortunetaly, I've lost the source code but still have the output PDF. Is there a convenient way to extract the original code from the presentation? Simple copy methods does not handle the mathematics well.
No, I don't think it is possible to do that. LaTeX is a typesetting language, in which you say "put a section here, this text here, some formulae here, etc., and use this style file to weight the fonts and spacing" and then compile it to PDF. The PDF document tells the PDF viewer (loosely speaking): "here's the font, place these sets of characters at these places in the document". It has no notion of section/heading/figure/equation/equation number etc.
It would be very hard to do PDF->LaTeX because of the multiple possibilities. i.e., LaTeX->PDF is a many-to-one function, so the inverse operation is going to have ambiguities.
For e.g., here's a test file using two different methods:
\documentclass{article}
\begin{document}
This is a StackOverflow test file.
\section{Method A}
\begin{equation}
ax^2+bx+c=0
\end{equation}
\end{document}
\documentclass{article}
\begin{document}
This is a StackOverflow test file.\\[0.1in]
\noindent {\Large \textbf{1\quad Method B}}
\begin{center}
$\displaystyle ax^2+bx+c=0$
\end{center}
\vspace{-0.25in}
\hfill{(1)}
\end{document}
You can see that you can't tell the two documents apart. A PDF to LaTeX converter will face the same problems.
That said, some word processing applications (open office?) can interpret PDF documents (usually only if all text) and convert it to a word document, and then you can convert that into LaTeX (usually provided by the same application). This might be one option worth trying. Other than that, there is no software that I know of that will do this for you.

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

Resources