4

If I use a command that begin a new block (according to AUCTeX), e.g.:

\newcommand\Test{\begin{abstract}}

And then I start my document:

\begin{document}

AUCTeX will indent my code as if I typed:

\begin{abstract}
  \begin{document}

Is there a way to tell AUCTeX to not consider command in newcommand for indentation?

Note: It may looks silly to declare a new command for \begin{abstract} but my real code is not about such command (simplified for the example). Actually the problem occurred while using the algorithm packages, and I wanted to define a new command such as IfAny (\newcommand\IfAny[1]{\If{\textbf{any} #1}) which does not really involved an environment.

Full example code after C-x h M-x indent-region:

\documentclass{article}
\newcommand\Test{\begin{abstract}}

  \begin{document}
\end{document}

The exact command concerns the algorithm and algpseudocode packages:

\newcommand\IfAny[1]{\If{\textbf{any }#1}}

Use like:

\IfAny{}
  \State ...
\EndIf

Which cannot be made into an environment (\newenvironment) unfortunately.

  • I've added a full example for reconstruction. It also shows the effect. – Tobias Mar 10 '16 at 9:27
  • The indentation error could be avoided by adding \newcommand\dummy{\end{abstract}}. A real solution would require a re-write of LaTeX-indent-calculate. Your\newcommand could be more complicated, e.g., span multiple lines. One needs a stack with indentation tokens such as a { symbols and begin-environment macros to match the begin and end-tokens. the begin-tokens push, the end-tokens pop. {-tokens have higher priority, i.e., if a } is read but an environment-token is on the stack the environment tokens are popped until an } is found and also popped. pop/push corresponds to indents – Tobias Mar 10 '16 at 10:25
  • A moderator deleted my not-quite-answer. The OP made a comment to that answer which I think should be visible to others, so I copy it here: “Actually the problem occurred while using the algorithm packages, and I wanted to define a new command such as IfAny (\newcommand\IfAny[1]{\If{\textbf{any} #1} ) which does not really involved an environment.” – Harald Hanche-Olsen Mar 10 '16 at 10:46
  • 1
    And more generally, TeX/LaTeX is such a flexible language, allowing all kinds of constructs that really clash with basic usage, that designing really robust automatic indentation support is a near impossible task. You'll either have to work around problems as they pop up due to bending the rules, or give up on using automated indentation altogether. – Harald Hanche-Olsen Mar 10 '16 at 10:53
  • 2
    What might have been nice, perhaps, is a pair of magic comments causing AUCTeX to not apply its indentation rules to stuff between them. Perhaps that's a reasonable feature request for the AUCTeX team? – Harald Hanche-Olsen Mar 10 '16 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.