[latex]e^{\i \pi} + 1 = 0[/latex]
* $latex [a, b]$ -> [latex][a, b][/latex]
*/
/**
* Markup LaTeX content.
*
* @param string $content Post or comment contents to markup.
*/
function latex_markup( $content ) {
$textarr = wp_html_split( $content );
$regex = '%
\$latex(?:=\s*|\s+)
((?:
[^$]+ # Not a dollar
|
(?<=(?', '"', "'", '&', '&', ' ', ' ' ), $latex );
}
/**
* Returns the URL for the server-side rendered image of LaTeX.
*
* @param string $latex LaTeX string.
* @param string $fg Foreground color.
* @param string $bg Background color.
* @param int $s Matches.
*
* @return string Image URL for the rendered LaTeX.
*/
function latex_render( $latex, $fg, $bg, $s = 0 ) {
$url = add_query_arg(
urlencode_deep(
array(
'latex' => $latex,
'bg' => $bg,
'fg' => $fg,
's' => $s,
'c' => '20201002', // cache buster. Added 2020-10-02 after server migration caused faulty rendering.
)
),
( is_ssl() ? 'https://' : 'http://' ) . 's0.wp.com/latex.php'
);
$alt = str_replace( '\\', '\', esc_attr( $latex ) );
return sprintf(
'',
esc_url( $url ),
$alt
);
}
/**
* The shortcode way. The attributes are the same as the old ones - 'fg' and 'bg', instead of foreground
* and background, and 's' is for the font size.
*
* Example: [latex s=4 bg=00f fg=ff0]\LaTeX[/latex]
*
* @param array $atts Shortcode attributes.
* @param string $content Content to format.
*/
function latex_shortcode( $atts, $content = '' ) {
$attr = shortcode_atts(
array(
's' => 0,
'bg' => latex_get_default_color( 'bg' ),
'fg' => latex_get_default_color( 'text', '000' ),
),
$atts,
'latex'
);
return latex_render( latex_entity_decode( $content ), $attr['fg'], $attr['bg'], $attr['s'] );
}
/**
* LaTeX needs to be untexturized.
*
* @param array $shortcodes Array of shortcodes not to texturize.
*/
function latex_no_texturize( $shortcodes ) {
$shortcodes[] = 'latex';
return $shortcodes;
}
add_filter( 'no_texturize_shortcodes', 'latex_no_texturize' );
add_filter( 'the_content', 'latex_markup', 9 ); // Before wptexturize.
add_filter( 'comment_text', 'latex_markup', 9 ); // Before wptexturize.
add_shortcode( 'latex', 'latex_shortcode' );