I am working on a book that has \documentclass{tufte-book}
as the document class. The book has code listings in it, some are full page width (they go into the side margin) and some are not. When using a listing, the caption and title for the listing go above the listing instead of in the side margin. Figures do have captions in the side margin and if you add the *
it makes the figure full width. I changed the name of figure titles to say "listing" instead so I could simply put the lstinputlisting
inside this and have all the same functionality as a listing but have it work like I want it. The issue just arose where I have code that is more than one page. A figure doesn't extend to the next page, it just cuts the figure off below the page. Is there a way to implement a listing to have the same functionality as a figure for a tufte-book document class (i.e. add *
for full width and captions in side margin) or have a figure span more than one page like a listing?
-
Welcome to TeX.SX! Are you asking about a breakable float or something non-floating?– TeXnicianSep 23, 2017 at 7:25
-
Figures will do this link and I renamed them to listings so that they would have the functionality I want but not be called figures. However they cannot extend past a full page. Is there a way to give a listing the properties a figure has in the tufte-book document class?– Chris UchytilSep 23, 2017 at 7:39
-
That was pretty clear, but you didn't answer the question, whether you want something that is able to float or something that stays in place?– TeXnicianSep 23, 2017 at 7:42
-
Oh I'm sorry I miss understood your question. By float do you mean it figures out where to put it on its own? The figures don't need to go exactly where the code is added to the document.– Chris UchytilSep 23, 2017 at 7:44
1 Answer
So this isn't exactly a solution I am happy with I guess it works for anyone else who finds this.
I did this
\addto\captionsenglish{\renewcommand{\figurename}{Listing}}
to change the name of the figure title to say "listing" to give a listing all the functionality a figure has in the tufte document class. As a heads up, an issue here is that if you also have figures it changes those names to "listing" as well. There might be a way around this by creating a custom command that just copies figures with a new name, not sure though.I did this
\usepackage{caption} \captionsetup{compatibility=false, font={small,rm}, labelfont={bf,rm}} \usepackage{subcaption}
to add caption functionality so I could use
\ContinuedFloat
. This overwrites the font style/format of the tufte style so you can change the font here to match what it used to be.I did this to split up a figure
\begin{figure*} \caption{apps/fdiff/shared.py} \label{apps/fdiff/shared} \begin{tcolorbox}[enlarge top by=9pt, enlarge bottom by=9pt, top=-9pt, bottom=-9pt,arc=0pt,outer arc=0pt,boxrule=0pt,colback=background] \lstinputlisting[language=python_custom, firstline=1, lastline=58]{fdiff/shared.py} \end{tcolorbox} \end{figure*} \clearpage \begin{figure*}\ContinuedFloat \caption{apps/fdiff/shared.py} \begin{tcolorbox}[enlarge top by=9pt, enlarge bottom by=9pt, top=-9pt, bottom=-9pt,arc=0pt,outer arc=0pt,boxrule=0pt,colback=background] \lstinputlisting[language=python_custom, firstline=59, lastline=75]{fdiff/shared.py} \end{tcolorbox} \end{figure*}
You have to manually adjust where the split is by adjusting the firstline
and lastline
parameters which is where it becomes annoying. I've found that about 60 ish lines fit on one page but this might need to be adjusted slightly to better fit function code blocks. Also the language
I have is a custom setup so that should be changed to the langue of your choice. The tcolorbox
is added because when I compiled the .tex to a pdf there were weird white space lines between each line. This might not be needed for you.