1 # -*- coding: utf-8 -*-
5 # Author: engelbert gruber <grubert@users.sourceforge.net>
6 # Copyright: This module has been placed in the public domain.
9 Tests for latex2e writer.
13 from __init__
import DocutilsTestSupport
16 settings
= {'use_latex_toc': False}
17 s
= DocutilsTestSupport
.PublishTestSuite('latex', suite_settings
=settings
)
18 s
.generateTests(totest
)
19 settings
['use_latex_toc'] = True
20 s
.generateTests(totest_latex_toc
)
21 settings
['use_latex_toc'] = False
22 settings
['sectnum_xform'] = False
23 s
.generateTests(totest_latex_sectnum
)
24 settings
['sectnum_xform'] = True
25 settings
['use_latex_citations'] = True
26 s
.generateTests(totest_latex_citations
)
27 settings
['table_style'] = ['colwidths-auto']
28 s
.generateTests(totest_table_style_auto
)
29 settings
['table_style'] = ['booktabs']
30 s
.generateTests(totest_table_style_booktabs
)
31 settings
['stylesheet_path'] = 'data/spam,data/ham.tex'
32 s
.generateTests(totest_stylesheet
)
33 settings
['embed_stylesheet'] = True
34 settings
['warning_stream'] = ''
35 s
.generateTests(totest_stylesheet_embed
)
38 head_template
= string
.Template(
39 r
"""$head_prefix% generated by Docutils <http://docutils.sourceforge.net/>
40 \usepackage{cmap} % fix search and cut-and-paste in Acrobat
42 %%% Custom LaTeX preamble
44 %%% User specified packages and stylesheets
46 %%% Fallback definitions for Docutils-specific commands
54 head_prefix
= r
"""\documentclass[a4paper]{article}
56 requirements
= r
"""\usepackage{ifthen}
57 \usepackage[T1]{fontenc}
58 \usepackage[utf8]{inputenc}
60 latex_preamble
= r
"""% PDF Standard Fonts
61 \usepackage{mathptmx} % Times
62 \usepackage[scaled=.90]{helvet}
65 longtable
= r
"""\usepackage{longtable,ltcaption,array}
66 \setlength{\extrarowheight}{2pt}
67 \newlength{\DUtablewidth} % internal use in tables
71 fallbacks_highlight
= r
"""% basic code highlight:
72 \providecommand*\DUrolecomment[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}}
73 \providecommand*\DUroledeleted[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}}
74 \providecommand*\DUrolekeyword[1]{\textbf{#1}}
75 \providecommand*\DUrolestring[1]{\textit{#1}}
77 % inline markup (custom roles)
78 % \DUrole{#1}{#2} tries \DUrole#1{#2}
79 \providecommand*{\DUrole}[2]{%
80 % backwards compatibility: try \docutilsrole#1{#2}
81 \ifcsname docutilsrole#1\endcsname%
82 \csname docutilsrole#1\endcsname{#2}%
84 \csname DUrole#1\endcsname{#2}%
90 \ifthenelse{\isundefined{\hypersetup}}{
91 \usepackage[colorlinks=true,linkcolor=blue,urlcolor=blue]{hyperref}
93 \urlstyle{same} % normal text font (alternatives: tt, rm, sf)
98 head
= head_template
.substitute(parts
)
100 head_table
= head_template
.substitute(
101 dict(parts
, requirements
= parts
['requirements'] + parts
['longtable']))
103 head_booktabs
= head_template
.substitute(
104 dict(parts
, requirements
=parts
['requirements']
105 + '\\usepackage{booktabs}\n' + parts
['longtable']))
107 head_textcomp
= head_template
.substitute(
108 dict(parts
, requirements
= parts
['requirements'] +
109 r
"""\usepackage{textcomp} % text symbol macros
112 head_alltt
= head_template
.substitute(
113 dict(parts
, requirements
= parts
['requirements'] +
114 r
"""\usepackage{alltt}
119 totest_latex_toc
= {}
120 totest_latex_sectnum
= {}
121 totest_latex_citations
= {}
122 totest_stylesheet
= {}
123 totest_stylesheet_embed
= {}
124 totest_table_style_auto
= {}
125 totest_table_style_booktabs
= {}
127 totest
['url_chars'] = [
128 ["http://nowhere/url_with%28parens%29",
130 \url{http://nowhere/url_with\%28parens\%29}
136 totest
['textcomp'] = [
137 ["2 µm is just 2/1000000 m",
139 2 µm is just 2/1000000 m
145 totest
['spanish quote'] = [
146 [".. role:: language-es\n\nUnd damit :language-es:`basta`!",
147 head_template
.substitute(dict(parts
, requirements
=
148 r
"""\usepackage{ifthen}
149 \usepackage[T1]{fontenc}
150 \usepackage[utf8]{inputenc}
151 \usepackage[spanish,english]{babel}
152 \AtBeginDocument{\shorthandoff{.<>}}
154 Und damit \foreignlanguage{spanish}{basta}!
160 totest
['code role'] = [
162 head_template
.substitute(dict(parts
, requirements
= parts
['requirements']+
163 r
"""\usepackage{color}
164 """, fallbacks
= parts
['fallbacks_highlight'])) + r
"""
165 \texttt{\DUrole{code}{x=1}}
171 totest
['table_of_contents'] = [
174 .. contents:: Table of Contents
185 head_template
.substitute(dict(parts
,
186 requirements
=parts
['requirements'] + '\\setcounter{secnumdepth}{0}\n',
188 % title for topics, admonitions, unsupported section levels, and sidebar
189 \providecommand*{\DUtitle}[2][class-arg]{%
190 % call \DUtitle#1{#2} if it exists:
191 \ifcsname DUtitle#1\endcsname%
192 \csname DUtitle#1\endcsname{#2}%
194 \smallskip\noindent\textbf{#2}\smallskip%
198 \phantomsection\label{table-of-contents}
199 \pdfbookmark[1]{Table of Contents}{table-of-contents}
200 \DUtitle[contents]{Table of Contents}
203 \item \hyperref[title-1]{Title 1}
206 \item \hyperref[title-2]{Title 2}
228 totest
['footnote text'] = [
235 .. [3] 1. enumeration
238 head_template
.substitute(dict(parts
,
239 fallbacks
=r
"""% numeric or symbol footnotes with hyperlinks
240 \providecommand*{\DUfootnotemark}[3]{%
241 \raisebox{1em}{\hypertarget{#1}{}}%
242 \hyperlink{#2}{\textsuperscript{#3}}%
244 \providecommand{\DUfootnotetext}[4]{%
246 \renewcommand{\thefootnote}{%
247 \protect\raisebox{1em}{\protect\hypertarget{#1}{}}%
248 \protect\hyperlink{#2}{#3}}%
253 \DUfootnotetext{id1}{id1}{1}{%
257 \DUfootnotetext{id2}{id2}{2}{}
259 \DUfootnotetext{id3}{id3}{3}{
269 totest_latex_toc
['no_sectnum'] = [
278 head_template
.substitute(dict(parts
,
279 requirements
=parts
['requirements'] + '\\setcounter{secnumdepth}{0}\n'
281 \phantomsection\label{contents}
282 \pdfbookmark[1]{Contents}{contents}
286 \section{first section%
287 \label{first-section}%
294 totest_latex_toc
['sectnum'] = [
304 head_template
.substitute(dict(parts
,
305 requirements
=parts
['requirements'] + '\\setcounter{secnumdepth}{0}\n'
307 \phantomsection\label{contents}
308 \pdfbookmark[1]{Contents}{contents}
312 \section{1 first section%
313 \label{first-section}%
321 totest_latex_sectnum
['no_sectnum'] = [
330 head_template
.substitute(dict(parts
, requirements
= parts
['requirements'] +
331 r
"""\setcounter{secnumdepth}{0}
336 \section{first section%
337 \label{first-section}%
344 totest_latex_sectnum
['sectnum'] = [
355 head_template
.substitute(dict(parts
,
356 requirements
=parts
['requirements'] + '\\setcounter{secnumdepth}{0}\n'
361 \section{first section%
362 \label{first-section}%
369 totest_latex_citations
['citations_with_underscore'] = [
372 Just a test citation [my_cite2006]_.
375 The underscore is mishandled.
379 Just a test citation \cite{my_cite2006}.
381 \begin{thebibliography}{my\_cite2006}
382 \bibitem[my\_cite2006]{my_cite2006}{
383 The underscore is mishandled.
385 \end{thebibliography}
392 totest_latex_citations
['adjacent_citations'] = [
395 Two non-citations: [MeYou2007]_[YouMe2007]_.
397 Need to be separated for grouping: [MeYou2007]_ [YouMe2007]_.
399 Two spaces (or anything else) for no grouping: [MeYou2007]_ [YouMe2007]_.
401 But a line break should work: [MeYou2007]_
405 .. [YouMe2007] important.
409 Two non-citations: {[}MeYou2007{]}\_{[}YouMe2007{]}\_.
411 Need to be separated for grouping: \cite{MeYou2007,YouMe2007}.
413 Two spaces (or anything else) for no grouping: \cite{MeYou2007} \cite{YouMe2007}.
415 But a line break should work: \cite{MeYou2007,YouMe2007}.
417 \begin{thebibliography}{MeYou2007}
418 \bibitem[MeYou2007]{MeYou2007}{
421 \bibitem[YouMe2007]{YouMe2007}{
424 \end{thebibliography}
431 totest
['enumerated_lists'] = [
435 2. Second to the previous item this one will explain
442 (I) having pre and postfixes
443 (II) in roman numerals.
450 \item Second to the previous item this one will explain
455 \renewcommand{\labelenumi}{\alph{enumi})}
463 \setcounter{enumi}{2}
469 \renewcommand{\labelenumi}{(\Roman{enumi})}
470 \item having pre and postfixes
472 \item in roman numerals.
480 # TODO: need to test for quote replacing if the language uses "ASCII-quotes"
481 # as active character (e.g. de (ngerman)).
484 totest
['table_caption'] = [
496 \setlength{\DUtablewidth}{\linewidth}
497 \begin{longtable}[c]{|p{0.075\DUtablewidth}|p{0.075\DUtablewidth}|}
510 totest
['table_styles'] = [
522 \setlength{\DUtablewidth}{\linewidth}
523 \begin{longtable*}[c]{p{0.075\DUtablewidth}p{0.075\DUtablewidth}}
547 \setlength{\DUtablewidth}{\linewidth}
548 \begin{longtable*}[c]{p{0.075\DUtablewidth}p{0.028\DUtablewidth}}
562 :class: colwidths-auto
569 \begin{longtable*}[c]{|l|l|}
586 \begin{longtable*}[c]{|l|l|}
603 \setlength{\DUtablewidth}{\linewidth}
604 \begin{longtable*}[c]{|p{0.191\DUtablewidth}|p{0.365\DUtablewidth}|}
618 totest_table_style_booktabs
['table_styles'] = [
619 # borderless overrides "booktabs" table_style
631 \setlength{\DUtablewidth}{\linewidth}
632 \begin{longtable*}[c]{p{0.075\DUtablewidth}p{0.075\DUtablewidth}}
656 \begin{longtable*}[c]{ll}
673 \setlength{\DUtablewidth}{\linewidth}
674 \begin{longtable*}[c]{p{0.191\DUtablewidth}p{0.365\DUtablewidth}}
687 totest_table_style_auto
['table_styles'] = [
699 \begin{longtable*}[c]{ll}
715 \begin{longtable*}[c]{ll}
723 # given width overrides "colwidth-auto"
733 \setlength{\DUtablewidth}{\linewidth}
734 \begin{longtable*}[c]{|p{0.191\DUtablewidth}|p{0.365\DUtablewidth}|}
748 totest
['table_align'] = [
759 \setlength{\DUtablewidth}{\linewidth}
760 \begin{longtable*}[r]{|p{0.075\DUtablewidth}|p{0.075\DUtablewidth}|}
774 totest
['table_empty_thead_entry'] = [
784 \setlength{\DUtablewidth}{\linewidth}
785 \begin{longtable*}[c]{|p{0.075\DUtablewidth}|p{0.086\DUtablewidth}|}
798 \multicolumn{2}{c}{\hfill ... continued on next page} \\
813 # The "[" needs to be protected (otherwise it will be seen as an
814 # option to "\\", "\item", etc. ).
816 totest
['bracket_protection'] = [
819 * [no option] to this item
823 \item {[}no option{]} to this item
830 totest
['literal_block'] = [
833 Test special characters { [ \\\\ ] } in literal block::
840 Test special characters \{ {[} \textbackslash{} {]} \} in literal block:
844 \{ [ ( \textbackslash{}macro
880 totest
['title_with_inline_markup'] = [
885 This is the *Subtitle*
886 ----------------------
888 This is a *section title*
889 ~~~~~~~~~~~~~~~~~~~~~~~~~
891 This is the *document*.
893 head_template
.substitute(dict(parts
,
894 requirements
=parts
['requirements'] + '\\setcounter{secnumdepth}{0}\n',
896 % subtitle (in document title)
897 \providecommand*{\DUdocumentsubtitle}[1]{{\large #1}}
899 pdfsetup
=parts
['pdfsetup'] + r
"""\hypersetup{
900 pdftitle={This is the Title},
902 """, titledata
=r
"""%%% Title Data
903 \title{\phantomsection%
904 This is the \emph{Title}%
905 \label{this-is-the-title}%
907 \DUdocumentsubtitle{This is the \emph{Subtitle}}%
908 \label{this-is-the-subtitle}}
911 """)) + r
"""\maketitle
914 \section{This is a \emph{section title}%
915 \label{this-is-a-section-title}%
918 This is the \emph{document}.
924 totest_stylesheet
['two-styles'] = [
926 ["""two stylesheet links in the header""",
927 head_template
.substitute(dict(parts
, stylesheet
=
928 r
"""\usepackage{data/spam}
931 two stylesheet links in the header
937 totest_stylesheet_embed
['two-styles'] = [
939 ["""two stylesheets embedded in the header""",
940 head_template
.substitute(dict(parts
, stylesheet
=
941 r
"""% Cannot embed stylesheet 'data/spam.sty':
942 % No such file or directory.
943 % embedded stylesheet: data/ham.tex
944 \newcommand{\ham}{wonderful ham}
947 two stylesheets embedded in the header
953 if __name__
== '__main__':
955 unittest
.main(defaultTest
='suite')