Custom list of things in LaTeX? - latex

I'm writing a script for college where I insert the date of a part of text apart from the chapters using a custom command (currently just displaying a marginpar), like:
\lesson{1}
\section{A}
\section{B}
\subsection{C}
\lesson{2}
\subsection{D}
Apart from the usual \listoffigures, \listoftables and \tableofcontents I want a list that allows access by lesson number. It should look like the \listoffigures except with a custom label (since the lessons don't have captions), for example:
Lesson 1 ..... Page 1
Lesson 2 ..... Page 5
Is there a package that allows me to define new lists like that as easy as creating new counters? Or do I have to dig into the source for the existing lists and hack my own?
(the memoir package documentation has a list of rendered examples at the beginning, that would be another example of a custom list like I need it)
Any keywords to google for are appreciated!

I would do the following:
1) Open a file at the beginning:
\newwrite\listoffoo
\immediate\openout\listoffoo=\jobname.foo
2) Each command like \lesson should put a line into the file:
\newcounter{lesson}
\def\lesson{%
...
\refstepcounter{lesson}%
\immediate\write\listoffoo{%
\string\lessonfooline{\ref{lesson}}{\pageref{lesson}}}
...
}
3) At the end of the processing, close the file and read it in:
\immediate\closeout\listoffoo
\input\jobname.foo
You'll have to define the commands like \lessonfooline.
Hope this outline helps.

Related

Use exams2pdf or a modified solution.tex template to print solutions not another answer list

I am trying to create a solution pdf for an exam but all I need for it is a simple marked box and not the answer list that follows. For example, I just want the first page from the following compilation and nothing else.
library(exams)
exams2pdf(list(
"boxplots",
c("tstat", "ttest", "confint"),
c("regression", "anova"),
"scatterplot",
"relfreq"
), template = "solution"
)
I don't have a strong grasp of the Latex language so when I try removing for example
\newenvironment{answerlist}{\renewcommand{\labelenumi}{(\alph{enumi})}\begin{enumerate}}{\end{enumerate}}
or
% \exinput{exercises}
It doesn't compile and just complains about a missing item etc
Which part of solution.tex can I remove that retains just the first page?
Solution: You need to delete all of the following lines from your copy of solution.tex:
\newpage
\begin{enumerate}
%% \exinput{exercises}
\end{enumerate}
Details: This LaTeX code starts a new page and includes an enumerated list for the individual exercises. The \exinput{exercises} is replaced with each of the {question} (and optionally {solution}) environments which then produces an \item for each question. So if you just remove the \exinput line, LaTeX complains about missing items because the {enumerate} list is empty.
Documentation: More details for exams2pdf() and its LaTeX templates can be found in vignette("exams", package = "exams").

natbib author-year citation - fallback to editor possible when author missing?

I'm having an issue with the natbib package. When the bib entry is missing an author I want to display the editor instead. However the second citation just takes the first three letters of the bib-key instead of the author.
Sample tex:
\documentclass{article}
\usepackage[authoryear,round]{natbib}
\bibliographystyle{abbrvnat}
\begin{document}
I need this \citet[S.~2]{Test1} to look like Surname (2022, S. 2).
and this \citep{Test1} like (Surname, 2022)
But also this \citet[S.~2]{Test2} to look like Editor (2022, S. 2).
and this \citep{Test2} like (Editor, 2022)
but it just prints (Tes, 2022)
\bibliography{refs}
\end{document}
The bib file could look like this:
#misc{Test1,
author={Surname, Name}
title = {Sometitle},
year = {2022}
}
#misc{Test2,
title = {Someothertitle},
editor={{Editor, Name}}
year = {2022}
}
Can anyone help with this? I'd also switch to another package than natbib, since I'm still early on the project. But at a first glance it seemed to meet my needs quite well.
Also I don't really know what I'm doing, it's been a while since I've been working with Latex. So sorry for that in advance!
Although the user posting the initial question here was able to switch to biblatex (which is good), not everyone has that freedom for some publishing venues. If this applies to you (that you have to stay with natbib and plainnat, you may be interested in a TeX Stack Exchange post that provides a description of how you can customize your BST file to modify your sorting, including configuring misc to sort by author then editor.
EDIT: A down-check for this answer being unhelpful inspired me to be more specific than my original answer above so the additional details specific to this case are below.
Since your provided code has natbib using abbrvnat (not plainnat as in the linked example that I initially provided), you can find abbrvnat.bst in your TeX repository and place a local copy with a different name (like abbrvnat-copy.bst) into your working directory. There are two steps in modifying your local copy of abbrvnat-copy.bst: First, update the calc.short.authors function to also look for an editor in the misc type. Second, have your TeX file use your newly customized BST file.
Note: I am using the 2007 version of abbrvnat.bst, which is the latest as of this posting in 2022.
In your local copy of abbrvnat-copy.bst, search for the calc.short.authors function. We'll need to add two lines of code to it:
type$ "misc" =
or
I show the new version here:
FUNCTION {calc.short.authors}
{ type$ "book" =
type$ "inbook" =
or
type$ "misc" =
or
'author.editor.key.label
{ type$ "proceedings" =
'editor.key.organization.label
{ type$ "manual" =
'author.key.organization.label
'author.key.label
if$
}
if$
}
if$
'short.list :=
}
In your TeX file, you'll need to change this line to account for your newly customized file: \bibliographystyle{abbrvnat-copy}. Other than this one line in your main .tex file and adding a couple missing commas in your .bib file, I did not change any code in your MWE.
Here's the output:
Other aspects that you may want to address (that you didn't ask about) include getting the editor(s) to appear in the bibliography (references list) for misc entries and sorting by editor for misc entries. The example that I provided here and the example provided in the other post that I previously linked should both be helpful in customizing your BST file to achieve your desired bibliography style.

How to add a (large) code appendix in LaTeX / LyX?

I'd like to add a code appendix to my LyX document. There are a few options I already considered, but they all have their problems.
I know a bit about listings, but one problem with those is that, if I copy & paste my code into them, I lose all enters/newlines. Since the code is too large to correct by hand, I was wondering if there is an alternative.
In LyX there is the possibility of inserting child documents, but that seems to be only for .tex files. Would have been ideal if I could just insert my .java file as a child document.
I could print the code to PDF, but it will include margins that mess up the final document, since the PDF is placed on the left margin of the final document and then there is the margin of the PDF. Also, this PDF always contains the entire code and white areas where not the entire page has been filled.
Does anyone have good alternative?
The listings package found here
http://www.ctan.org/tex-archive/macros/latex/contrib/listings/
allows the include of external source code files (look into the reference for \lstinputlisting).
EDIT: here you find some samples how to use it:
http://en.wikibooks.org/wiki/LaTeX/Packages/Listings
If you need to copy-paste code to LyX listing box then use Edit -> Paste Special -> Seletion or Ctrl+Alt+V.
For what it's worth, at least the 2.0 versions of LyX have the ability to include listings as child documents. Insert, File, Child Document, and choose from the dropdown box "Program Listing". This uses the listings package and lets you keep your source in its own file.
If listings doesn't support your language, you can always use something like highlight or source-highlight to generate a latex snippet of syntax-highlighted code that you can add as a child document of type "Input"
Yes, if you copy&paste code into the LyX listings box, you lose all newlines, but you can preprocess your code (insert an extra newline below each line):
$ cat foo.java | sed -e 's/$/\n/' > bar.java
Then you can copy&paste the new file bar.java and everything will be ok.

How to Print Rails Source Code?

I'd like to read the Rails 3 source code on printed paper (and preferably in color).
For example, xv6 did a nice job printing their code. It even has line numbers and an index. The only thing I would like to add is syntax highlighting.
Anyone know how any of this is possible?
Here are two possibilities I found:
1. The Listings Package (could this also generate other formats besides PDF, like HTML?)
ftp://ftp.tex.ac.uk/tex-archive/macros/latex/contrib/listings/listings.pdf o
2. Highlight (does it do indexing?)
http://www.andre-simon.de/
We could even add a rake task, print, that generates an up-to-date PDF.
I recomend you use highlight to turn the code into LaTeX and then use Listings to make it into a PDF, then print!

Incorrect hyperlinking in LaTeX

So far the hyperref package in LaTeX has automatically linked all the items in my report correctly. But there is one that did not get linked properly, possibly because I manually added it to the table of contents like this:
\addcontentsline{toc}{chapter}{Bibliography}
How do I fix this?
You have to use the \phantomsection macro just before or after the \addtocontentsline. This instructs hyperref to add an anchor at that point. Also, take into account that you have to run LaTeX several times to make this anchor to be in the exact place.
If you're using a standard LaTeX document class, such as report, you can use the tocbibind package, and then you won't need the \addcontentsline command and the hyperlinks should work correctly. From the documentation:
The tocbibind package enables the titles of the Table of Contents, the List of Figure, the List of Tables, the Bibliography and the Index all to be added to the Table of Contents.
I'd also suggest looking at the memoir class, which also solves this problem. It's intended to be a replacement for the standard report and book classes, and provides lots of options and commands for customisation.

Resources