diff --git a/src/parser/latex_fragment.rs b/src/parser/latex_fragment.rs index 84dfae8..67d3b79 100644 --- a/src/parser/latex_fragment.rs +++ b/src/parser/latex_fragment.rs @@ -23,7 +23,11 @@ pub fn latex_fragment<'r, 's>( context: Context<'r, 's>, input: &'s str, ) -> Res<&'s str, LatexFragment<'s>> { - let (remaining, _) = alt((parser_with_context!(raw_latex_fragment)(context),))(input)?; + let (remaining, _) = alt(( + parser_with_context!(raw_latex_fragment)(context), + parser_with_context!(escaped_parenthesis_fragment)(context), + parser_with_context!(escaped_bracket_fragment)(context), + ))(input)?; let (remaining, _) = space0(remaining)?; let source = get_consumed(input, remaining); Ok((remaining, LatexFragment { source })) @@ -72,3 +76,41 @@ fn brackets<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &' ))(input)?; Ok((remaining, body)) } + +#[tracing::instrument(ret, level = "debug")] +fn escaped_parenthesis_fragment<'r, 's>( + context: Context<'r, 's>, + input: &'s str, +) -> Res<&'s str, &'s str> { + let (remaining, _) = tag("\\(")(input)?; + let (remaining, _) = recognize(many_till( + anychar, + peek(alt(( + parser_with_context!(exit_matcher_parser)(context), + tag("\\)"), + ))), + ))(remaining)?; + let (remaining, _) = tag("\\)")(remaining)?; + + let source = get_consumed(input, remaining); + Ok((remaining, source)) +} + +#[tracing::instrument(ret, level = "debug")] +fn escaped_bracket_fragment<'r, 's>( + context: Context<'r, 's>, + input: &'s str, +) -> Res<&'s str, &'s str> { + let (remaining, _) = tag("\\[")(input)?; + let (remaining, _) = recognize(many_till( + anychar, + peek(alt(( + parser_with_context!(exit_matcher_parser)(context), + tag("\\]"), + ))), + ))(remaining)?; + let (remaining, _) = tag("\\]")(remaining)?; + + let source = get_consumed(input, remaining); + Ok((remaining, source)) +}