O3PO_Latex
extends O3PO_Latex_Dictionary_Provider
in package
This class provides a collection of various functions to transform, parse, and convert latex code to and from utf8 and other encodings.
Tags
Table of Contents
Properties
- $additional_default_macros : array<string|int, mixed>
- Additional macros.
Methods
- expand_cite_to_html() : mixed
- Expand \cite{} commands to html code.
- expand_latex_macros() : mixed
- Expand LaTeX macros.
- extract_abstracts() : mixed
- Extract all abstracts from latex code.
- extract_bibliographies() : mixed
- Extract all bibliographies from latex code.
- extract_latex_macros() : mixed
- Extracts LaTeX command definitions from latex code.
- get_latex_clean_up_dictionary() : mixed
- Get the $latex_clean_up_dictionary.
- get_latex_special_chars_dictionary() : mixed
- Get the $latex_special_chars_dictionary.
- get_latex_special_chars_reverse_dictionary() : mixed
- Get the $latex_special_chars_reverse_dictionary.
- get_month_string() : mixed
- Get the BibTeX string representation of a numeric month
- get_special_macros_to_ignore_in_bbl() : mixed
- Get special macros we sometimes want to ignore in expansion.
- get_utf8_to_ascii_dictionary() : mixed
- Get the $utf8_to_ascii_dictionary.
- latex_to_utf8_outside_math_mode() : string
- Convert LaTeX code to utf8, leaving alone everything within math mode.
- normalize_whitespace_and_linebreak_characters() : mixed
- Normalized white space and line break characters.
- parse_bbl() : mixed
- Parse bbl code of potentially multiple bibliographies.
- parse_single_bbl() : mixed
- Parse bbl code of an individual bibliography.
- preg_match_latex_math_mode_delimters() : mixed
- Preg match LaTeX math mode delimters.
- preg_match_outside_math_mode() : int
- Preg match in latex code, but only taking into account the part of code that is not in math mode.
- preg_split_at_latex_math_mode_delimters() : mixed
- Preg split LaTeX code at math mode delimters.
- remove_font_changing_commands() : mixed
- Remove font changing commands.
- remove_special_macros_to_ignore_in_bbl() : mixed
- Remove special macros we sometimes want to ignore in expansion.
- strpos_outside_math_mode() : mixed
- Strpos in latex code, but only taking into account the part of code that is not in math mode. This function uses the multi byte safe mb_str_pos() function.
- title_to_key_suffix() : mixed
- Compute a string suitable for taking the role of the BibTeX key from a text such as a title.
- un_escape_url() : string
- Un-escape a LaTeX style escaped url.
- utf8_to_bibtex() : mixed
- Convert utf8 text to LaTeX code suitable for BibTeX.
- utf8_to_closest_latin_letter_string() : mixed
- Convert utf8 strings to the closes latin letter string.
- utf8_to_latex() : mixed
- Convert utf8 text to LaTeX code.
Properties
$additional_default_macros
Additional macros.
private
static array<string|int, mixed>
$additional_default_macros
= array(array('', '\newcommand', '\@firstoftwo', '[2]', '', '#1'), array('', '\newcommand', '\@secondoftwo', '[2]', '', '#2'), array('', '\newcommand', '\@gobble', '[1]', '', ''), array('', '\newcommand', '\@gobbletwo', '[2]', '', ''))
Array of default macros that we always want to expand, irrespective of whether they appeared in a LaTeX file.
Array of additional macros that we often want to expand even if they were not explicitely defined by the user.
Tags
Methods
expand_cite_to_html()
Expand \cite{} commands to html code.
public
static expand_cite_to_html(string $text, string $bbl) : mixed
The resulting html code makes every \cite a clickable hyperlink to the correspnding entry in the bibliograhy.
Parameters
- $text : string
-
The text in which \cite commands are to be expanded
- $bbl : string
-
The bbl code of the bibliography that contains the corresponding bibliography entries.
Tags
expand_latex_macros()
Expand LaTeX macros.
public
static expand_latex_macros(array<string|int, mixed> $macro_definitions, string $text) : mixed
Attempts to expands all macros in $text for which a definition is given in $macro_definitions.
Expects definitions like those produced by self::extract_latex_macros().
Parameters
- $macro_definitions : array<string|int, mixed>
-
Array of macro definitions.
- $text : string
-
Text containing LaTeX macros that are to be expanded.
Tags
extract_abstracts()
Extract all abstracts from latex code.
public
static extract_abstracts(string $latex) : mixed
Parameters
- $latex : string
-
Latex code to search for abstracts.
Tags
extract_bibliographies()
Extract all bibliographies from latex code.
public
static extract_bibliographies(string $latex) : mixed
Parameters
- $latex : string
-
Latex code to search for bibliographies.
Tags
extract_latex_macros()
Extracts LaTeX command definitions from latex code.
public
static extract_latex_macros(string $latex_source) : mixed
Extracts LaTeX command definitions from latex code.
Supported are:
newcommand providecommand renewcommand renewcommand. def DeclarePairedDelimiter
For a command such as
\newcommand{\myvec}[1]{\vec{#1}}
the resulting definition is of the following form:
Full match 0-22 \newcommand{\myvec}[1]
Group 1. 1-11 newcommand
Group 2. 12-18 \myvec
Group 3. 19-22 [1]
Group 4. 22-22 `` (in case a default is set making an argument optional)
Group 5. 23-31 \vec{#1}
Parameters
- $latex_source : string
-
LaTeX source code from with the macro definitions are to be extracted.
Tags
get_latex_clean_up_dictionary()
Get the $latex_clean_up_dictionary.
public
static get_latex_clean_up_dictionary() : mixed
Tags
get_latex_special_chars_dictionary()
Get the $latex_special_chars_dictionary.
public
static get_latex_special_chars_dictionary() : mixed
We are dealing with a lot of LaTeX code in wich special characters are LaTeX encoded. On our website we want to display them in a pretty way. The following is a (more or less complete) mapping of LaTeX encodigs into utf-8 characters whenever such a character is available. This list must only contain latex macros that contain a \ as for preformance reasons replacing is stopped once all \ have been eliminated. Also there is a negative look must be added when using this dictionary to prevent replacements in cases such as \v{a}.
Tags
get_latex_special_chars_reverse_dictionary()
Get the $latex_special_chars_reverse_dictionary.
public
static get_latex_special_chars_reverse_dictionary() : mixed
Tags
get_month_string()
Get the BibTeX string representation of a numeric month
public
static get_month_string(string $month) : mixed
Parameters
- $month : string
-
Number of the month in the range 1-12.
Tags
get_special_macros_to_ignore_in_bbl()
Get special macros we sometimes want to ignore in expansion.
public
static get_special_macros_to_ignore_in_bbl() : mixed
Some macros are better kept unexpended even if the authors have manually re-defined them, because we need them to more efficiently parse the latex code (i.e., to identify DOIs or URLs).
Tags
get_utf8_to_ascii_dictionary()
Get the $utf8_to_ascii_dictionary.
public
static get_utf8_to_ascii_dictionary() : mixed
Tags
latex_to_utf8_outside_math_mode()
Convert LaTeX code to utf8, leaving alone everything within math mode.
public
static latex_to_utf8_outside_math_mode(string $latex_text[, bool $clean = true ]) : string
Does not attempt to deal with commands that cannot be reasonably represented in utf8, such as \small, \newblock, \emph, ...
Leaves mathematical formulas enclosed in $...$ (and other math mode delimiters such as $$...$$,...) intact so that they can be displayed nicely using MathJax. This function turns various the math modes into linline mode $a+b$.
Parameters
- $latex_text : string
-
Latex code whose non-math part is to be converted to utf8
- $clean : bool = true
-
Whether to perform some cleanup at the end.
Tags
Return values
string —A utf8 approximation to $latex_text
normalize_whitespace_and_linebreak_characters()
Normalized white space and line break characters.
public
static normalize_whitespace_and_linebreak_characters(string $text[, bool $single_line = true ][, bool $remove_extra_newlines = false ]) : mixed
Parameters
- $text : string
-
LaTeX text to normalize.
- $single_line : bool = true
-
Whether to output a single-line text.
- $remove_extra_newlines : bool = false
-
If true, single newlines are replaced by space and any number of more than two successive newlines are replaced by exactly two newlines.
Tags
parse_bbl()
Parse bbl code of potentially multiple bibliographies.
public
static parse_bbl(string $bbl) : mixed
Parses bbl code produced by either bibtex or biblatex as well as such written by authors by hand. $bbl may be a concatenation of multiple bibliographies.
Parameters
- $bbl : string
-
Bibliography or concatenation of bibliographies in .bbl format as it is produced by BibTeX, Biber, and BibLaTeX.
Tags
parse_single_bbl()
Parse bbl code of an individual bibliography.
public
static parse_single_bbl(string $bbl) : mixed
Parses bbl code produced by either bibtex or biblatex as well as such written by authors by hand.
Parameters
- $bbl : string
-
Bibliography in .bbl format as it is produced by BibTeX, Biber, and BibLaTeX.
Tags
preg_match_latex_math_mode_delimters()
Preg match LaTeX math mode delimters.
public
static preg_match_latex_math_mode_delimters(string $text[, array<string|int, mixed> &$matches = null ][, int $flags = 0 ][, mixed $offset = 0 ]) : mixed
Parameters
- $text : string
-
Text to be split at LaTeX math mode delimiters such as $...$ [...] (...).
- $matches : array<string|int, mixed> = null
-
If provided is filled with matches.
- $flags : int = 0
-
Flags can be any combination of valid preg_match flags. Defaults to 0.
- $offset : mixed = 0
Tags
preg_match_outside_math_mode()
Preg match in latex code, but only taking into account the part of code that is not in math mode.
public
static preg_match_outside_math_mode(string $pattern, string $subject[, array<string|int, mixed> &$matches = array() ][, int $flags = 0 ][, int $offset = 0 ]) : int
Parameters
- $pattern : string
-
Reular expression to match against.
- $subject : string
-
Latex text in whose non-math parts the expression is to be found.
- $matches : array<string|int, mixed> = array()
-
If matches is provided, then it is filled with the results of search. $matches[0] will contain an array of texts that matched the full pattern, $matches[1] will have an array of the texts that matched the first captured parenthesized subpattern, and so on.
- $flags : int = 0
-
Flags as in preg_match().
- $offset : int = 0
-
Place from which to start the search (in bytes) within each segment of subjbect that is outisde math mode.
Tags
Return values
int —False in case an error occurred during any of the matches. Alternatively returns the total number of segments in which a match was found.
preg_split_at_latex_math_mode_delimters()
Preg split LaTeX code at math mode delimters.
public
static preg_split_at_latex_math_mode_delimters(string $text[, int $limit = -1 ][, int $flags = 0 ]) : mixed
If $text is well formed, Odd parts of the resulting array are outside math mode and even parts are inside math mode. If $fags is not specified or set to 0 the delimiters are stripped.
Parameters
- $text : string
-
Text to be split at LaTeX math mode delimiters such as $...$ [...] (...).
- $limit : int = -1
-
If specified, then only substrings up to limit are returned with the rest of the string being placed in the last substring. A limit of -1 or 0 means "no limit" and, as is standard across PHP, you can use NULL to skip to the flags parameter.
- $flags : int = 0
-
Flags can be any combination of valid preg_split flags. Defaults to 0.
Tags
remove_font_changing_commands()
Remove font changing commands.
public
static remove_font_changing_commands(string $text) : mixed
Parameters
- $text : string
-
LaTeX text to remove font commands from.
Tags
remove_special_macros_to_ignore_in_bbl()
Remove special macros we sometimes want to ignore in expansion.
public
static remove_special_macros_to_ignore_in_bbl(array<string|int, mixed> $latex_macro_definitions) : mixed
Parameters
- $latex_macro_definitions : array<string|int, mixed>
-
Array of latex macro definitions.
Tags
strpos_outside_math_mode()
Strpos in latex code, but only taking into account the part of code that is not in math mode. This function uses the multi byte safe mb_str_pos() function.
public
static strpos_outside_math_mode(string $latex_text, string $string) : mixed
Parameters
- $latex_text : string
-
Latex text in whose non-math parts the string is to be found.
- $string : string
-
String to be found.
Tags
title_to_key_suffix()
Compute a string suitable for taking the role of the BibTeX key from a text such as a title.
public
static title_to_key_suffix(string $text) : mixed
Parameters
- $text : string
-
Title of an article from which a BibTeX entry key is to be generated.
Tags
un_escape_url()
Un-escape a LaTeX style escaped url.
public
static un_escape_url(string $latex_url) : string
Parameters
- $latex_url : string
-
LaTeX style escaped url.
Tags
Return values
string —Url without LaTeX escape sequences.
utf8_to_bibtex()
Convert utf8 text to LaTeX code suitable for BibTeX.
public
static utf8_to_bibtex(string $text) : mixed
Parameters
- $text : string
-
Text with special characters that are to be converted to a LaTeX type encoding suitable for the use in BibTeX .bib files.
Tags
utf8_to_closest_latin_letter_string()
Convert utf8 strings to the closes latin letter string.
public
static utf8_to_closest_latin_letter_string(string $text) : mixed
Parameters
- $text : string
-
Text with utf8 special characters.
Tags
utf8_to_latex()
Convert utf8 text to LaTeX code.
public
static utf8_to_latex(string $text) : mixed
Parameters
- $text : string
-
Text with special characters that are to be converted to LaTeX encoding.