latex2e writer : Move usepackage hyperref after stylesheet inclusion.
[docutils.git] / test / test_writers / test_latex2e.py
blob84e263f59be0fd00794fb27364d86e34df2d4801
1 #! /usr/bin/env python
3 # $Id$
4 # Author: engelbert gruber <grubert@users.sourceforge.net>
5 # Copyright: This module has been placed in the public domain.
7 """
8 Tests for latex2e writer.
9 """
11 from __init__ import DocutilsTestSupport
13 def suite():
14 settings = {'use_latex_toc': 0}
15 s = DocutilsTestSupport.PublishTestSuite('latex', suite_settings=settings)
16 s.generateTests(totest)
17 settings['use_latex_toc'] = 1
18 s.generateTests(totest_latex_toc)
19 settings['use_latex_citations'] = 1
20 s.generateTests(totest_latex_citations)
21 return s
24 latex_head = """\
25 \\documentclass[10pt,a4paper,english]{article}
26 \\usepackage{babel}
27 \\usepackage{ae}
28 \\usepackage{aeguill}
29 \\usepackage{shortvrb}
30 \\usepackage[latin1]{inputenc}
31 \\usepackage{tabularx}
32 \\usepackage{longtable}
33 \\setlength{\\extrarowheight}{2pt}
34 \\usepackage{amsmath}
35 \\usepackage{graphicx}
36 \\usepackage{color}
37 \\usepackage{multirow}
38 \\usepackage{ifthen}
39 \\usepackage[DIV12]{typearea}
40 % generated by Docutils <http://docutils.sourceforge.net/>
41 \\newlength{\\admonitionwidth}
42 \\setlength{\\admonitionwidth}{0.9\\textwidth}
43 \\newlength{\\docinfowidth}
44 \\setlength{\\docinfowidth}{0.9\\textwidth}
45 \\newlength{\\locallinewidth}
46 \\newcommand{\\optionlistlabel}[1]{\\bf #1 \\hfill}
47 \\newenvironment{optionlist}[1]
48 {\\begin{list}{}
49 {\\setlength{\\labelwidth}{#1}
50 \\setlength{\\rightmargin}{1cm}
51 \\setlength{\\leftmargin}{\\rightmargin}
52 \\addtolength{\\leftmargin}{\\labelwidth}
53 \\addtolength{\\leftmargin}{\\labelsep}
54 \\renewcommand{\\makelabel}{\\optionlistlabel}}
55 }{\\end{list}}
56 \\newlength{\\lineblockindentation}
57 \\setlength{\\lineblockindentation}{2.5em}
58 \\newenvironment{lineblock}[1]
59 {\\begin{list}{}
60 {\\setlength{\\partopsep}{\\parskip}
61 \\addtolength{\\partopsep}{\\baselineskip}
62 \\topsep0pt\\itemsep0.15\\baselineskip\\parsep0pt
63 \\leftmargin#1}
64 \\raggedright}
65 {\\end{list}}
66 % begin: floats for footnotes tweaking.
67 \\setlength{\\floatsep}{0.5em}
68 \\setlength{\\textfloatsep}{\\fill}
69 \\addtolength{\\textfloatsep}{3em}
70 \\renewcommand{\\textfraction}{0.5}
71 \\renewcommand{\\topfraction}{0.5}
72 \\renewcommand{\\bottomfraction}{0.5}
73 \\setcounter{totalnumber}{50}
74 \\setcounter{topnumber}{50}
75 \\setcounter{bottomnumber}{50}
76 % end floats for footnotes
77 % some commands, that could be overwritten in the style file.
78 \\newcommand{\\rubric}[1]{\\subsection*{~\\hfill {\\it #1} \\hfill ~}}
79 \\newcommand{\\titlereference}[1]{\\textsl{#1}}
80 % end of "some commands"
81 \\ifthenelse{\\isundefined{\\hypersetup}}{
82 \\usepackage[colorlinks=true,linkcolor=blue,urlcolor=blue]{hyperref}
83 }{}
84 """
86 totest = {}
87 totest_latex_toc = {}
88 totest_latex_citations = {}
90 totest['table_of_contents'] = [
91 # input
92 ["""\
93 .. contents:: Table of Contents
95 Title 1
96 =======
97 Paragraph 1.
99 Title 2
100 -------
101 Paragraph 2.
102 """,
103 ## # expected output
104 latex_head + """\
105 \\title{}
106 \\author{}
107 \\date{}
108 \\raggedbottom
109 \\begin{document}
111 \\setlength{\\locallinewidth}{\\linewidth}
112 \\hypertarget{table-of-contents}{}
113 \\pdfbookmark[0]{Table of Contents}{table-of-contents}
114 \\subsubsection*{~\\hfill Table of Contents\\hfill ~}
115 \\label{table-of-contents}
116 \\begin{list}{}{}
117 \\item {} \\href{\\#title-1}{Title 1}
118 \\begin{list}{}{}
119 \\item {} \\href{\#title-2}{Title 2}
121 \\end{list}
123 \\end{list}
127 %___________________________________________________________________________
129 \\hypertarget{title-1}{}
130 \\pdfbookmark[0]{Title 1}{title-1}
131 \\section*{Title 1}
132 \\label{title-1}
134 Paragraph 1.
137 %___________________________________________________________________________
139 \\hypertarget{title-2}{}
140 \\pdfbookmark[1]{Title 2}{title-2}
141 \\subsection*{Title 2}
142 \\label{title-2}
144 Paragraph 2.
146 \\end{document}
147 """],
151 totest_latex_toc['table_of_contents'] = [
152 # input
153 ["""\
154 .. contents:: Table of Contents
156 Title 1
157 =======
158 Paragraph 1.
160 Title 2
161 -------
162 Paragraph 2.
163 """,
164 ## # expected output
165 latex_head + """\
166 \\title{}
167 \\author{}
168 \\date{}
169 \\raggedbottom
170 \\begin{document}
172 \\setlength{\\locallinewidth}{\\linewidth}
173 \\hypertarget{table-of-contents}{}
174 \\renewcommand{\contentsname}{Table of Contents}
175 \\tableofcontents
177 \\bigskip
178 \\label{table-of-contents}
181 %___________________________________________________________________________
183 \\hypertarget{title-1}{}
184 \\section{Title 1}
185 \\label{title-1}
187 Paragraph 1.
190 %___________________________________________________________________________
192 \\hypertarget{title-2}{}
193 \\subsection{Title 2}
194 \\label{title-2}
196 Paragraph 2.
198 \\end{document}
199 """],
204 totest_latex_citations['citations_with_underscore'] = [
205 # input
206 ["""\
207 Just a test citation [my_cite2006]_.
209 .. [my_cite2006]
210 The underscore is mishandled.
211 """,
212 ## # expected output
213 latex_head + """\
214 \\title{}
215 \\author{}
216 \\date{}
217 \\raggedbottom
218 \\begin{document}
220 \\setlength{\\locallinewidth}{\\linewidth}
222 Just a test citation \\cite{my_cite2006}.
224 \\begin{thebibliography}{my{\\_}cite2006}
225 \\bibitem[my{\\_}cite2006]{my_cite2006}{
226 The underscore is mishandled.
228 \\end{thebibliography}
230 \\end{document}
231 """],
235 totest['enumerated_lists'] = [
236 # input
237 ["""\
238 1. Item 1.
239 2. Second to the previous item this one will explain
241 a) nothing.
242 b) or some other.
244 3. Third is
246 (I) having pre and postfixes
247 (II) in roman numerals.
248 """,
249 # expected output
250 latex_head + """\
251 \\title{}
252 \\author{}
253 \\date{}
254 \\raggedbottom
255 \\begin{document}
257 \\setlength{\\locallinewidth}{\\linewidth}
258 \\newcounter{listcnt0}
259 \\begin{list}{\\arabic{listcnt0}.}
261 \\usecounter{listcnt0}
262 \\setlength{\\rightmargin}{\\leftmargin}
264 \\item {}
265 Item 1.
267 \\item {}
268 Second to the previous item this one will explain
270 \\end{list}
271 \\begin{quote}
272 \\setcounter{listcnt0}{0}
273 \\begin{list}{\\alph{listcnt0})}
275 \\usecounter{listcnt0}
276 \\setlength{\\rightmargin}{\\leftmargin}
278 \\item {}
279 nothing.
281 \\item {}
282 or some other.
284 \\end{list}
285 \\end{quote}
286 \\setcounter{listcnt0}{0}
287 \\begin{list}{\\arabic{listcnt0}.}
289 \\usecounter{listcnt0}
290 \\addtocounter{listcnt0}{2}
291 \\setlength{\\rightmargin}{\\leftmargin}
293 \\item {}
294 Third is
296 \\end{list}
297 \\begin{quote}
298 \\setcounter{listcnt0}{0}
299 \\begin{list}{(\\Roman{listcnt0})}
301 \\usecounter{listcnt0}
302 \\setlength{\\rightmargin}{\\leftmargin}
304 \\item {}
305 having pre and postfixes
307 \\item {}
308 in roman numerals.
310 \\end{list}
311 \\end{quote}
313 \\end{document}
314 """],
317 # BUG: need to test for quote replacing if language is de (ngerman).
319 totest['quote_mangling'] = [
320 # input
321 ["""\
322 Depending on language quotes are converted for latex.
323 Expecting "en" here.
325 Inside literal blocks quotes should be left untouched
326 (use only two quotes in test code makes life easier for
327 the python interpreter running the test)::
330 This is left "untouched" also *this*.
333 .. parsed-literal::
335 should get "quotes" and *italics*.
338 Inline ``literal "quotes"`` should be kept.
339 """,
340 latex_head + """\
341 \\title{}
342 \\author{}
343 \\date{}
344 \\raggedbottom
345 \\begin{document}
347 \\setlength{\\locallinewidth}{\\linewidth}
349 Depending on language quotes are converted for latex.
350 Expecting ``en'' here.
352 Inside literal blocks quotes should be left untouched
353 (use only two quotes in test code makes life easier for
354 the python interpreter running the test):
355 \\begin{quote}{\\ttfamily \\raggedright \\noindent
356 "{}"~\\\\
357 This~is~left~"untouched"~also~*this*.~\\\\
358 "{}"
359 }\\end{quote}
360 \\begin{quote}{\\ttfamily \\raggedright \\noindent
361 should~get~"quotes"~and~\\emph{italics}.
362 }\\end{quote}
364 Inline \\texttt{literal "quotes"} should be kept.
366 \\end{document}
367 """],
370 totest['table_caption'] = [
371 # input
372 ["""\
373 .. table:: Foo
375 +-----+-----+
376 | | |
377 +-----+-----+
378 | | |
379 +-----+-----+
380 """,
381 latex_head + """\
382 \\title{}
383 \\author{}
384 \\date{}
385 \\raggedbottom
386 \\begin{document}
388 \\setlength{\\locallinewidth}{\\linewidth}
390 \\leavevmode
391 \\begin{longtable}[c]{|p{0.075\locallinewidth}|p{0.075\locallinewidth}|}
392 \\caption{Foo}\\\\
393 \\hline
394 & \\\\
395 \hline
396 & \\\\
397 \hline
398 \\end{longtable}
400 \\end{document}
401 """],
404 totest['table_class'] = [
405 # input
406 ["""\
407 .. table::
408 :class: borderless
410 +-----+-----+
411 | 1 | 2 |
412 +-----+-----+
413 | 3 | 4 |
414 +-----+-----+
415 """,
416 latex_head + """\
417 \\title{}
418 \\author{}
419 \\date{}
420 \\raggedbottom
421 \\begin{document}
423 \\setlength{\\locallinewidth}{\\linewidth}
425 \\leavevmode
426 \\begin{longtable}[c]{p{0.075\locallinewidth}p{0.075\locallinewidth}}
431 \\\\
436 \\\\
437 \\end{longtable}
439 \\end{document}
440 """],
443 # In "\\\n[" the "[" needs to be protected (otherwise it will be seen as an option to "\\").
444 totest['brackett_protection'] = [
445 # input
446 ["""\
449 something before to get a end of line.
452 the empty line gets tested too
454 """,
455 latex_head + """\
456 \\title{}
457 \\author{}
458 \\date{}
459 \\raggedbottom
460 \\begin{document}
462 \\setlength{\\locallinewidth}{\\linewidth}
463 \\begin{quote}{\\ttfamily \\raggedright \\noindent
464 something~before~to~get~a~end~of~line.~\\\\
465 {[}~\\\\
466 ~\\\\
467 the~empty~line~gets~tested~too~\\\\
469 }\\end{quote}
471 \\end{document}
472 """],
475 totest['raw'] = [
476 ["""\
477 .. raw:: latex
479 \\noindent
481 A paragraph.
483 .. |sub| raw:: latex
485 (some raw text)
487 Foo |sub|
488 same paragraph.
489 """,
490 latex_head + """\
491 \\title{}
492 \\author{}
493 \\date{}
494 \\raggedbottom
495 \\begin{document}
497 \\setlength{\\locallinewidth}{\\linewidth}
498 \\noindent
499 A paragraph.
501 Foo (some raw text)
502 same paragraph.
504 \\end{document}
505 """],
508 if __name__ == '__main__':
509 import unittest
510 unittest.main(defaultTest='suite')