Open akern40 opened 2 months ago
Thanks for catching this! This bug makes sense to me, and I have encountered it before. As a temporary workaround, I recommend skipping the offending lines by appending % tex-fmt: skip
. A good general solution seems quite difficult: consider the following document.
\documentclass{article}
\begin{document}
Consider this (I have a long sentence in parentheses
over multiple lines, which should indent
appropriately, but also have numbered items
1) number one and 2) number two.
).
\end{document}
Let me know if you have any ideas for solving these types of issues: a perfect solution is probably impossible but any improvements are welcome.
Oh ya skipping is a good idea I hadn't thought of 😅
Personally I'm not sure I'd want to have any indentation caused by parentheses in non-math settings? I think this is how latexindent
treats it. But I can think of two schemes or heuristics that could help. I'm not super familiar with the code, so this could be far-fetched.
The simple heuristic is that close parentheses which appear after numbers, single letters, or Roman numerals are treated as non-matching.
The more complex scheme I imagine a bit like a state machine: when it finds an open parenthesis, it enters state Open
. Finding a close parenthesis triggers state MaybeClosed(1)
. Another close parenthesis triggers MaybeClosed(2)
, etc. This state persists until the program either a) finds an open parenthesis or b) hits the end of the document. At that point, the stack unwinds: the final MaybeClosed
is treated as a matching parenthesis, and all intermediates are treated as non-indenting.
Like you said, I think there is no "true" solution; the problem is definitionally ambiguous, which is why I'd kinda suggest sidestepping it entirely.
(Also: thank you for open source work! Don't want to be the guy who suggests a complex solution and doesn't offer implementation help. I'll just skip the line for now.)
I think that the implementation with Open
and MaybeClosed
will fail if the last closing parenthesis is not the matching one. Consider
This sentence (which I am writing right
here) has two parts: 1) the first part,
and 2) the second part.
This would receive indentation even though it should not. The option to simply ignore all parentheses outside maths mode could be good; I will look into an implementation of this if I have some time.
Ah yep good counterexample - ignoring would be great!
Linking #18 here, as some of the logic for detecting math mode could be useful.
I have a similar problem when using a), b)
style enumerate items
\begin{enumerate}[label=\alph*)] % this ) causes indentation to fail
\item foo % I expect this line to be indented
\end{enumerate}
I'm mentioning since it may be easy to fix this special case since the context of the offending )
is quite obvious
This package is excellent! I did run into an indentation bug, though: unmatched parentheses outside of a math environment cause a "dedent" (I think), which mucks up the indentation count for the rest of the document. As a minimal example, this correctly-formatted document:
gets incorrectly formatted by
tex-fmt
with the following output from
--trace
: