-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest2LaTeX.tex
More file actions
2593 lines (1606 loc) · 78.6 KB
/
test2LaTeX.tex
File metadata and controls
2593 lines (1606 loc) · 78.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%This notebook demonstrates the use of the workpackage template, replace with your own.
\documentclass[english]{workpackage}[1996/06/02]
% input the common preamble content (required by the ipnb2latex converter)
\input{header.tex}
% the following three lines are required to support the tikz examples
\usepackage{tikz}
\usepackage{sansmath}
\usetikzlibrary{shadings,intersections}
% then follows the rest of the preamble to be placed before the begin document
% this preamble content is special to the documentclass you defined above.
\WPproject{My Project} % project name
\WPequipment{Work package demonstrator} % equipment name
\WPsubject{How to use the converter to create a work package} % main heading
\WPconclusions{\begin{enumerate}
\item This work package demonstrates the use of the converter.
\item It was tested with and should work on Python 3.5 and up.
\end{enumerate}}
\WPclassification{Unclassified}
\WPdocauthor{CJ Willers}
\WPcurrentpackdate{\today}
\WPcurrentpacknumber{0001} % work package number
\WPdocnumber{} % this doc number hosts all the work packages
\WPprevpackdate{} % work package which this one supersedes
\WPprevpacknumber{} % work package which this one supersedes
\WPsuperpackdate{} % work package which comes after this one
\WPsuperpacknumber{} % work package which comes after this one
\WPdocontractdetails{false}
\WPcontractname{} % contract name
\WPorderno{} % contract order number
\WPmilestonenumber{} % contract milestone number
\WPmilestonetitle{} % contract milestone title
\WPcontractline{} % contract milestone line number
\WPdocECPnumber{1234567} % ecp/ecr number
\WPdistribution{
%\vspace{0.1mm}
%\begin{tabular}{lllll}
%Name 1 & Name 12 & Name 3 & Name 4 & Name 5\\
%Name 6 & Name 7 & \multicolumn{3}{l}{Master: some repo}\\
%\end{tabular}
}
% bibfile added in this notebook
%\addbibresource{./analyseRio.bib}
% this is entered just before the end{document}
\newcommand{\atendofdoc}{
\bibliographystyle{IEEEtran}
\bibliography{test2LaTeX}
}
%and finally the document begin.
\begin{document}
\WPlayout
% some Latex code to be used verbatim
\
\vspace{50mm}
\begin{center}
\includegraphics[width=0.6\textwidth]{./images/keep-calm-and-code-python_BW.png}
\end{center}
\vspace{5mm}
\begin{center}
{\LARGE ipnb2tex}
\end{center}
\newpage
\tableofcontents
\listoffigures
\listoftables
\lstlistoflistings
\chapter{The ipnb2tex.py Script}
\label{sec:Theipnb2texpyScript}
The \verb+ipnb2tex.py+ reads the IPython notebook and converts it to a \LaTeX{} set of files (a \verb+*.tex+ file and a number of images). The script is invoked as follows:
\verb+python ipnb2tex.py file.ipynb file.tex imagedir -i -u -b+
where
\begin{itemize}
\item \verb+file.ipynb+ [optional] is the name of the input IPython notebook file. If no input filename is supplied, all \verb+.ipynb+ files in current directory will be processed. In this event the output filenames will be the same as the \verb+.ipynb+ files, just with a \verb+tex+ filetype. The notebook filename must not have spaces.
\item \verb+file.tex+ [optional] is the name of output \LaTeX{} file. If none is given the output filename will be the same as the input file, but with the \verb+.tex+ extension.
\item \verb+imagedir+ [optional] is the directory where images are written to. If not given, this image directory will be the \verb+./pic+ directory.
\item \verb+-i+ [optional], the lower case letter \verb+i+, if this option is given the code listings are printed inline with the body text where they occur, otherwise listings are floated to the end of the document.
\item \verb+-u+ [optional] add \verb+\url{}+ to the bibtex entries, to obtain \verb+url = {\url{http://ipython.org/}}+, otherwise use the form
\verb+url = {http://ipython.org/}+
\item \verb+-b+ [optional] if given merge all the *.bib files in the folder root together. By default, don't merge all bibtex files, only use the \verb+file.bib+ file with the same name as the notebook file.
\end{itemize}
The options must follow after the filenames and dirname and the end of the command line string.
\section{Known deficiencies}
\label{sec:Knowndeficiencies}
\begin{enumerate}
\item Some complex cell-merged HTML tables may not render correctly in LaTeX (let me know if you have such a table).
\item The following HTML elements are not currently processed, these elements are simply ignored: \verb+div+, \verb+img+.
\item The \verb+iframe+ HTML element is tested for embedded PDF files which are then included as an image.
\item Coloured text in HTML is not exported as coloured text in LaTeX.
\item Many reserved LaTeX symbols such as hash, caret, underscore and dollar are 'legal' in normal markdown. When rendering to LaTeX these symbols cause errors unless escaped with backslash. In many cases these symbols are escaped, but not always because of context. If the symbols are escaped, they render incorrectly in normal Markdown. Therefore, choose your target renderer and enter the symbols accordingly, accepting problems in the alternative renderer.
\end{enumerate}
\chapter{Heading 1 nb2pdf}
\label{sec:Heading1nb2pdf}
Heading 1 is a \LaTeX{} chapter.
Headings down to level 5 (\LaTeX{} paragraph) are supported.
Headings at level 6 are treated as normal body text.
If you don't need a chapter (i.e., for an article), just don't use a heading at level 1.
When any level of section heading is formed, a label is automatically created from the text in the heading, removing all non-alpha characters. If the section heading is very long the the label will also be long, so keep it short. Also, don't use the same text twice as a heading.
\section{Heading 2 Second-level heading}
\label{sec:Heading2Secondlevelheading}
\subsection{Heading 3}
\label{sec:Heading3}
\subsubsection{Heading 4}
\label{sec:Heading4}
\paragraph{Heading 5}
\label{sec:Heading5}
\subparagraph{Heading 6}
\label{sec:Heading6}
some more markdown.
\chapter{Heading X}
\label{sec:HeadingX}
\section{Heading X2}
\label{sec:HeadingX2}
\subsection{Heading X3}
\label{sec:HeadingX3}
\subsubsection{Heading X4}
\label{sec:HeadingX4}
\paragraph{Heading X5}
\label{sec:HeadingX5}
\subparagraph{Heading X6}
\label{sec:HeadingX6}
\section{LaTeX Template Format and the Header File}
\label{sec:LaTeXTemplateFormatandtheHeaderFile}
\subsection{Default Style (Report)}
\label{sec:DefaultStyleReport}
The notebook can be converted to the LaTeX \verb+Report+ format with no additional files or document class. A 'standard' notebook, where the first cell is not \verb+Raw NBConvert+ will be prepended with a short section of LaTeX code for a standard LaTeX Report. A number of pacakges are also \verb+\usepackage{}+ed, as required by the converted LaTeX code.
\subsection{User-Defined Style}
\label{sec:UserDefinedStyle}
The notebook can optionally be converted to a user-defined format with minimal effort. The example files included the GitHub distribution provides a 'work package' format class file (\verb+workpackage.cls+) and a header file (\verb+header.tex+).
The first cell of the notebook must be a \verb+RawNBConvert+ cell with at least the following contents (see the first cell of this notebook for an example):
\begin{verbatim}
\documentclass that you want to use
\input{header.tex}
whatever preamble lines you require
\begin{document}
\end{verbatim}
Then follows whatever content you need to use your document style, e.g., font style. Write/change this file to change the front matter and appearance of the document. The script adds the \verb+\end{document}+ line after the notebook cells' contents.
The \verb+header.tex+ file provides the functionality required by the converter-created code. This content is input using the \LaTeX{} input command. A number of pacakges are \verb+\usepackage{}+ed, as required by the converted LaTeX code.
\section{Company Logo}
\label{sec:CompanyLogo}
The work package template has a \verb+logo.png+ file which is used to define a logo on top of the page. The present image is empty, so no logo will show. You can add your own logo to this file.
\section{The images directory}
\label{sec:Theimagesdirectory}
The script saves the png files in the \verb+./pic+ directory, and the \LaTeX{} code also expects to find the png files there. Hence, there must be an existing \verb+./pic+ directory in the directory where you run the script. If an alternative name is given on the command line, the alternative name will be used.
\section{Using Python code}
\label{sec:UsingPythoncode}
See Listing~\ref{lst:listing1} for the code to prepare the environment.
\begin{lstlisting}[style=outcellstyle,caption={Caption for first listing output \label{lst:listing1-out}}]
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
\end{lstlisting}
The following code block has the metadata
\begin{verbatim}
{
"listingCaption": {
"caption": "Caption text for second listing",
"label": "lst:listing2"
}
}
\end{verbatim}
which will give the code block a caption and a label, but the output is not captioned.
The code block also demonstrates how long lines are handled in this lstlistings configuration, with a linefeed symbol to mark the line.
The output of the following cell is captured in a floating listing that can be referenced. The metadata is as follows:
\begin{verbatim}
{
"listingCaption": {
"caption": "Caption text for second listing",
"label": "lst:listingrad",
"outputCaption": "Output caption text for second listing"
}
}
\end{verbatim}
If the meta data has an \verb+entry and a+ entry the
\begin{lstlisting}[style=outcellstyle,caption={Output caption text for second listing \label{lst:listingrad-out}}]
Radius = 6
Frontal area = 7 m2
This is a very long line of python code, that is supposed to flow over to the next line, in order to test the listing display
\end{lstlisting}
See Listing~\ref{lst:listing3} for the code to print the value.
\begin{lstlisting}[style=outcellstyle]
a
b
\end{lstlisting}
\begin{lstlisting}[style=outcellstyle]
0
1
\end{lstlisting}
The following code block has no metadata, and hence no captions on either the code or the output listings.
See Listing~\ref{lst:autolistingcell30} for the code.
\begin{lstlisting}[style=outcellstyle]
code with no listing caption
\end{lstlisting}
Output emanating from different cells \textit{may} result in different output listings in \LaTeX{} even if they appear in a single output box in the notebook. The cells below attempt to recreate a similar scenario in a different application, but it does not seem to repeat the problem here.
See Listing~\ref{lst:autolistingcell32} for the code.
See Listing~\ref{lst:autolistingcell33} for the code.
\begin{lstlisting}[style=outcellstyle]
function output 1
function output 2
function output 3
9
3
\end{lstlisting}
\section{Raw NBConvert cells}
\label{sec:RawNBConvertcells}
The \verb+Raw NBConvert+ cell type can be used to propagate text without any modification to the output stream. The IPython notebook will not touch this text; it is passed on as-is to the converter code. This this case we can add \LaTeX{} code that will progress straight through to the \LaTeX{} compiler.
This raw capability is very useful if the notebook is intended to be exported to \LaTeX{} as primary output. I find myself writing most of such documentation in raw latex in a 'Raw NBConvert' cell. In this mode most of the notebook cells are never rendered to HTML, since you are reading and editing the \LaTeX{} and it stays in \LaTeX{} for display purposes as well. So you get the full benefit of all \LaTeX{} functionality (except that it is not rendered inside the notebook) and all the other benefits of the notebook.
Raw NBConvert cell. This could be raw \LaTeX{} as in
\begin{equation}
f_{WN}(\theta;\mu, \sigma) = \frac{1}{\sigma\sqrt{2\pi}}\sum^\infty_{k=-\infty} \exp\left[\frac{-(\theta-\mu+2\pi k)^2}{2\sigma^2}\right]
\end{equation}
\section{Captions}
\label{sec:Captions}
There are three objects that can have captions: listings, figures and tables.
Figures and tables without captions are non-floating and appear in the text at the position it is found in the input file. Figures and tables with captions are floating.
Listings are normally non-floating but can be floated (see below).
A single cell can contain any combination of captions, and these are indicated in the metadata field of the cell.
It seems that the means to access the metadata changes with every new release. Try one of the following:
\begin{enumerate}
\item Select the \verb+Edit Metadata+ drop-down option in the \verb+Cell Toolbar+ drop-down box (towards the right of the toolbar). This should expose metadata buttons on all the cells. Click on the button and type in the metadata.
\item On the left panel click on the Property Inspector (gear icon) and open the \verb+Advanced Tools+ drop-down to expose the \verb+Cell Metadata+ text box.
\end{enumerate}
Enter the meta data into the text box. Ensure that the tabs and commas are all in the right places: if so, a small tick mark will appear for you to approve the new metadata, by clicking on the tick mark. If the tabs and commas are not exactly right, the text box boundary will be red (error) and the tick mark will not be shown.
If the json structure has no errors, a tick mark will appear: click on the tick mark to save the changes (as long as there are errors you cannot save).
The metadata has the following structure:
\begin{verbatim}
{
"listingCaption": {
"caption": "Comparison of effective drag parameters",
"label": "lst:comparedrag",
"outputCaption": ""
},
"figureCaption": {
"caption": "Caption text for first figure",
"label": "fig:lab1",
"width": "0.2",
"locator": "t"
},
"tableCaption": {
"caption": "Caption text for first table",
"label": "tab:lab1",
"format": "{|p{10mm}|l|r|c|c|p{50mm}|p{20mm}|}",
"fontsize": "normalsize",
"locator": "t"
}
}
\end{verbatim}
Note the placement of commas. No newlines are allowed in a single string in the metadata.
\section{Python code}
\label{sec:Pythoncode}
Python code blocks are printed using the \LaTeX{} \verb+listings+ package. The format details can be defined in the header template file. These code listings can be supplied with a caption and a label (but these code blocks are not floating entities).
\begin{verbatim}
{
"listingCaption": {
"caption": "Caption text for first listing",
"label": "lst:listing1",
"outputCaption": "Caption for first listing output"
}
}
\end{verbatim}
where the different fields have the following meanings:
\begin{enumerate}
\item \verb+caption+ specifies the text to be used in the listings caption. If this metadata field is not supplied, the listing will not be given a caption.
\item \verb+label+ specifies the label to be used for the listing, which can be used in \LaTeX{} to reference the listing. The code block uses the label exactly as given in the metadata field. If the output listing is given an \verb+outputCaption+, the output caption is labelled by the a label starting with the \verb+label+ string given, but appended with the string \verb+-out+ (i.e., \verb+lst:listing1-out+ in this case).
\item \verb+outputCaption+ specifies the label that is used for the output listing of the cell. If this metadata field is not supplied, the output listing will not be given a caption.
\item Both the cell listing and the output listing share the same root label, but the output listing has \verb+-out+ appended to it.
\end{enumerate}
We can refer to Listings~\ref{lst:listing1}, \ref{lst:listingrad-out}, \ref{lst:listingrad}, and \ref{lst:listing3}.
The default is to provide colour syntax highlighting. If you don't want colour in the listings, simply remove these lines from \verb+header.tex+
\begin{verbatim}
commentstyle=\color{mygreen}, \% comment style
keywordstyle=\color{blue}, \% keyword style
stringstyle=\color{mymauve}, \% string literal style
\end{verbatim}
\section{Floated code listings}
\label{sec:Floatedcodelistings}
In order to improve readability in the document, the code listings can be floated to the end of the document by using the \verb+-l+ switch on the command line. If this switch is used, the listing is appended to the \LaTeX{} document and the end of the conversion. At the location in the document where the code listing would have been, a reference sentence to the code is included. The sentence is constructed as follows:
\verb+See Listing~\ref{lst:autolistingcellX} for the code {purpose}.+
where the symbolic link is constructed using the cell number and the text used in the \verb+{purpose}+ part is taken from the first line of the code, if the first line is a comment with a single \verb+#+ followed by a space. Hence if the code in cell 3 starts with the lines
\begin{verbatim}
# to prepare the environment
import numpy as np
\end{verbatim}
a text line of the following form will be created:
\verb+See Listing~\ref{lst:autolistingcell3} for the code to prepare the environment.+
Just maintain the discipline to write good comments in the first line, keeping in mind that the comment will end up in the body of the document with the reference.
If a code cell does not start with a comment, no text will be inserted and the form will be
\verb+See Listing~\ref{lst:autolistingcell24} for the code.+
If the code cell starts with a comment of the form \verb+##+, the listing will output to the tex file, but there is no text entry in the location where the code cell was. i.e., no line starting with \verb+See Listing+...
If the code cell starts with a comment of the form \verb+###+, neither the listing nor the text entry referring to it is written to the latex file. So if you don't want the code to be shown or a reference to it made, start the first code line with \verb+###+.
\section{Figure captions and bitmaps}
\label{sec:Figurecaptionsandbitmaps}
PNG images, read in from externally or created by Matplotlib, are imported into the \LaTeX{} code with lines of the following form:
\verb+\includegraphics[width=WIDTH\textwidth]{./pngs/test2LaTeX\_21\_0.png}+
where the WIDTH determines the size of the image (obtained from metadata, see below), and the image file name is constructed from the notebook file name and cell information. The name is the concatenation of the notebook file name, the cell index (index number of the cell in the notebook file) and the sub-index image in the cell's output (there can be more than one image). All indices are zero based.
The width is read from the metadata, which must be structured as follows:
\begin{verbatim}
{
"figureCaption": {
"caption": "Caption text for first figure",
"label": "fig:lab1",
"width": "0.2",
"locator": "t"
},
"listingCaption": {
"caption": "Listing caption in cell with a figure",
"label": "lst:figurelisting",
"outputCaption": "Output for a cell with a figure"
}
}
\end{verbatim}
Width is the fraction of \LaTeX{} textwidth, i.e., if width=1, the graphic will be the full width of the text on the page.
The locator value is used in \verb+\begin{figure}[tb]+ or \verb+\begin{table}[tb]+ to locate the float on the page. Default value is 'tb', can be any combination of \verb+tbhp+.
The metadata also contains a caption string, and a label for the figure. The type must be \verb+figure+.
If the \verb+caption+ field is given (or has non-zero length), the image will be written to a \LaTeX{} float with a caption (and label). If the \verb+caption+ fields is not given, the image will not be encapsulated in the floating figure.
If the caption string contains a substring \verb+(###)+ the substring will be replaced with the figure index (plus one) for the current cell. Hence, if there are multiple figures in the same cell, they can all use the same caption with with a running number, such as (1), (2), etc.
Latex special characters in the caption strings must be properly escaped as per standard LaTeX use, it is not translated or manipulated in any way. For example, underscore \verb`_` must be escaped.
However, keep in mind that JSON requires backslash to be escaped itself as a double backslash in order to survive the JSON parser. So if \verb`_` is required in the final document, it must be \verb`\\_` in the caption string. For exanple \verb`a\\_a\\_a b\\#b\\%b` in the JSON metadata becomes \verb`a_a_a b#b%b` in the final \LaTeX{} caption, as shown in Figure~\ref{fig:lab1}.
See Listing~\ref{lst:figurelisting} for the code this figure is given meta data caption parameters, it will be floated in the latex export.
{
\begin{figure}[tb]
\centering
\begin{normalsize}
\includegraphics[width=0.2\textwidth, angle=0]{./pic/test2LaTeX_43_0.png}
\end{normalsize}
\caption{Caption text for first figure, having a\_a\_a b\#b\%b\label{fig:lab1-0}\label{fig:lab1}}
\end{figure}
}
\begin{lstlisting}[style=outcellstyle,caption={Output for a cell with a figure \label{lst:figurelisting-out}}]
also force print output
\end{lstlisting}
Experimenting with embedded markdown images:
\begin{center}
\includegraphics[width=0.9\textwidth, angle=0]{./images/keep-calm-and-code-python_BW.png}
\end{center}
The following figure is not floating, but with narrower width.
\begin{verbatim}
{
"figureCaption": {
"width": "0.3"
}
}
\end{verbatim}
See Listing~\ref{lst:autolistingcell46} for the code this figure is not given meta data caption parameters, it will be inlined in the latex export.
\begin{center}
\includegraphics[width=0.9\textwidth, angle=0]{./pic/test2LaTeX_46_0.png}
\end{center}
The following Matplotlib plot is scaled to 40\% of text width and floating, but with the 'h' locator to force the location to here.
\begin{verbatim}
{
"figureCaption": {
"caption": "Caption text for third figure",
"label": "fig:lab3",
"width": "0.4",
"locator": "h"
}
}
\end{verbatim}
{
\begin{figure}[tpbh]
\centering
\begin{normalsize}
\includegraphics[width=0.4\textwidth, angle=0]{./pic/test2LaTeX_48_0.png}
\end{normalsize}
\caption{Caption text for third figure\label{fig:lab3-0}\label{fig:lab3}}
\end{figure}
}
\FloatBarrier
JSON does not seem to like backslashes, so math embedded in the figure caption must be escaped with two backslashes, as shown in Figure~\ref{fig:lab3math}.
\begin{lstlisting}
{
"figureCaption": {
"caption": "Caption with math $\\pi$ $\\beta$, $\\rho$",
"label": "fig:lab3math",
"locator": "h",
"width": "0.4"
}
}
\end{lstlisting}
{
\begin{figure}[tbph]
\centering
\begin{normalsize}
\includegraphics[width=0.4\textwidth, angle=0]{./pic/test2LaTeX_52_0.png}
\end{normalsize}
\caption{Caption with math $\pi$ $\beta$, $\rho$\label{fig:lab3math-0}\label{fig:lab3math}}
\end{figure}
}
It is possible to have more than one plot in a single cell output. In this case each output can be made floating separately or be non-floated (you can even mix floating and non-floating). The captions are given in the metadata in the following format:
\begin{verbatim}
{
"figureCaption": {
"caption": "['Caption text for (first) fourth figure','',
'Caption text for (third) fourth figure']",
"label": "fig:lab4",
"width": "[0.5, 0.5, 0.5]"
}
}
\end{verbatim}
In this case the caption strings and scaling values are allocated to the sequence of images in the order given. If a caption string is zero length, that specific image will be non-floating (as in the example of instance \verb+b+ here).
The float labels are determined from the root value given in the metadata, but with the output sub-index appended, as in \verb+fig:lab4-0+, \verb+fig:lab4-1+, \verb+fig:lab4-2+.
The first image in a cell has two labels \verb+fig:lab4-0+ and \verb+fig:lab4+ (without the zero) so either form can be used.
If you only have one figure in a cell there is no need to add the \verb+-0+. There two references should be the same:
\ref{fig:lab4-0} and \ref{fig:lab4} because they are both attached to the first figure in the cell.
See Listing~\ref{lst:autolistingcell54} for the code.
{
\begin{figure}[tb]
\centering
\begin{normalsize}
\includegraphics[width=0.4\textwidth, angle=0]{./pic/test2LaTeX_54_0.png}
\end{normalsize}
\caption{Caption text for (first) fourth figure\label{fig:lab4-0}\label{fig:lab4}}
\end{figure}
}
\begin{center}
\includegraphics[width=0.9\textwidth, angle=0]{./pic/test2LaTeX_54_1.png}
\end{center}
{
\begin{figure}[tb]
\centering
\begin{normalsize}
\includegraphics[width=0.5\textwidth, angle=0]{./pic/test2LaTeX_54_2.png}
\end{normalsize}
\caption{Caption text for (third) fourth figure\label{fig:lab4-2}}
\end{figure}
}
To use mathematics in cells with two outputs as in the following code, the backslashes must be escaped as for a single caption, but the strings in the list must also be marked as raw strings as in \verb+r'string'+
\begin{lstlisting}
{
"figureCaption": {
"caption": "[r'Caption text for $\\alpha$ figure',r'Caption text for $\\beta$ figure']",
"label": "fig:lab4alpha",
"width": "[0.3, 0.5]"
}
}
\end{lstlisting}
See Listing~\ref{lst:autolistingcell57} for the code.
{
\begin{figure}[tb]
\centering
\begin{normalsize}
\includegraphics[width=0.3\textwidth, angle=0]{./pic/test2LaTeX_57_0.png}
\end{normalsize}
\caption{Caption text for $\alpha$ figure\label{fig:lab4alpha-0}\label{fig:lab4alpha}}
\end{figure}
}
{
\begin{figure}[tb]
\centering
\begin{normalsize}
\includegraphics[width=0.5\textwidth, angle=0]{./pic/test2LaTeX_57_1.png}
\end{normalsize}
\caption{Caption text for $\beta$ figure\label{fig:lab4alpha-1}}
\end{figure}
}
\clearpage
Multiple pictures in the same cell can share the same caption, appended with (1), (2), etc.
Use the substring \verb+(###)+ as a placeholder to be replaced with the running count number.
See Listing~\ref{lst:autolistingcell60} for the code.
{
\begin{figure}[tbp]
\centering
\begin{normalsize}
\includegraphics[width=0.2\textwidth, angle=0]{./pic/test2LaTeX_60_0.png}
\end{normalsize}
\caption{Shared caption (1)\label{fig:sharedcap-0}\label{fig:sharedcap}}
\end{figure}
}
{
\begin{figure}[tbp]
\centering
\begin{normalsize}
\includegraphics[width=0.2\textwidth, angle=0]{./pic/test2LaTeX_60_1.png}
\end{normalsize}
\caption{Shared caption (2)\label{fig:sharedcap-1}}
\end{figure}
}
{
\begin{figure}[tbp]
\centering
\begin{normalsize}
\includegraphics[width=0.2\textwidth, angle=0]{./pic/test2LaTeX_60_2.png}
\end{normalsize}
\caption{Shared caption (3)\label{fig:sharedcap-2}}
\end{figure}
}
\FloatBarrier % message to latex, no floats past this barrier
You can write HTML to the cell's output and have it displayed in the browser and also in the LaTeX file.
See Listing~\ref{lst:autolistingcell63} for the code.
\begin{center}
\includegraphics[width=0.9\textwidth, angle=0]{./images/flow2-zzz.png}
\end{center}
There might be a picture before this text if the HTML pictures are exporting :-)
Experimenting with embedded markdown images. The figures should be
Figure~\ref{fig:keepcalm-0} and
Figure~\ref{fig:keepcalm-1}.
{
\begin{figure}[t]
\centering
\begin{normalsize}
\includegraphics[width=0.1\textwidth, angle=0]{./images/keep-calm-and-code-python_BW.png}
\end{normalsize}
\caption{Keep calm 1\label{fig:keepcalm-0}\label{fig:keepcalm}}
\end{figure}
}
{
\begin{figure}[t]
\centering
\begin{normalsize}
\includegraphics[width=0.2\textwidth, angle=0]{./images/random-squares-2.png}
\end{normalsize}
\caption{Keep calm 2\label{fig:keepcalm-1}}
\end{figure}
}
\section{Graphs as PDF documents}
\label{sec:GraphsasPDFdocuments}
Reports with many png images tends to create large PDF files. If the graphs are exported as PDF files by the backend, the files are vector graphics and not large bitmaps, resulting in a much smaller final PDF report file.
The PDF export functionality worked well in brief testing, but it still needs more testing.
\begin{enumerate}
\item Set up the notebook to use the PDF backend:
\%config InlineBackend.figure\_format = 'pdf'
\item With the PDF backend the graphs are saved as PDF documents in the notebook. This converter script will export these as PDF files to the pictures directory.
\item Compile the document with PDFLaTeX as usual. It will look for the graphics files as PDF files, not PNG files.
\end{enumerate}
\section{Embedded PDF documents}
\label{sec:EmbeddedPDFdocuments}
You can embed a PDF file in a notebook for display in the notebook with the following:
\begin{verbatim}
from IPython.display import IFrame
IFrame("./images/discretestrata01.pdf", width=600, height=300)
IFrame("./images/Bankedturn.pdf", width=600, height=300)
\end{verbatim}
This currently does not work in Chrome, but it does work in Firefox, provided you set the browser to open PDF files in the browser and not download the files.
The above code displays only the second iframe in the browser, it seems to overwrite the first.
The converter will export both of the above PDF filenames for display in the latex document.
{
\begin{figure}[tbp]
\centering
\begin{normalsize}
\includegraphics[width=0.5\textwidth, angle=0]{./images/discretestrata01.pdf}
\end{normalsize}
\caption{Stratified atmosphere\label{fig:templabel-0}\label{fig:templabel}}
\end{figure}
}
{
\begin{figure}[tbp]
\centering
\begin{normalsize}
\includegraphics[width=0.5\textwidth, angle=0]{./images/Bankedturn.pdf}
\end{normalsize}
\caption{Banked turn\label{fig:templabel-1}}
\end{figure}
}
\section{Cells with Python errors}