Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extra level of indentation in tikzpictures #1960

Closed
IngoMeyer441 opened this issue Feb 16, 2021 · 7 comments 路 Fixed by #1970
Closed

Extra level of indentation in tikzpictures #1960

IngoMeyer441 opened this issue Feb 16, 2021 · 7 comments 路 Fixed by #1970

Comments

@IngoMeyer441
Copy link

Hey and thanks for this fantastic vim plugin! 馃槃 vimtex is great for working with LaTeX documents but I found a minor issue regarding the indentation rules in tikz pictures. Consider this minimal example:

\documentclass{scrreprt}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
    \node at (0, 0) {
        \includegraphics{logo.png}
    };
\end{tikzpicture}
\end{document}

Every time I enter } to close the \includegraphics command, vimtex adds one extra level of indentation although the command is already indented. I tried to understand what s:indent_delims does but I am not quite sure how it works or could be fixed.

@lervag
Copy link
Owner

lervag commented Feb 18, 2021

Hey and thanks for this fantastic vim plugin! smile vimtex is great for working with LaTeX documents

Thanks for the kind words!

but I found a minor issue regarding the indentation rules in tikz pictures. Consider this minimal example: ...

Just to be sure: You are not complaining about the result if I take the example file and do gg=G, right? On my end, the result looks like this:

\documentclass{scrreprt}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \node at (0, 0) {
    \includegraphics{logo.png}
    };
\end{tikzpicture}
\end{document}

Every time I enter } to close the \includegraphics command, vimtex adds one extra level of indentation although the command is already indented.

I can't properly reproduce the problem you are describing. Can you provide a full minimal example and a more detailed description?

@IngoMeyer441
Copy link
Author

You can reproduce it with this minimal vimrc:

set nocompatible

call plug#begin('~/.vim/plugged')
Plug 'lervag/vimtex'
call plug#end()

filetype plugin indent on
syntax on
set autoindent
set smarttab
set shiftwidth=4
set softtabstop=4
set tabstop=4

Create a new tex file (I used test.tex), and paste the whole TikZ example from above:

\documentclass{scrreprt}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
    \node at (0, 0) {
        \includegraphics{logo.png}
    };
\end{tikzpicture}

Save and exit Vim. Reopen the file, navigate to the ending of the \includegraphics line, enter insert mode and delete the brace }. Then enter the brace } again. You should now get an extra level of indentation although the indentation was already correct.

I have tested this in Vim 8.2 and NeoVim 0.4.4 on Arch Linux.

@lervag
Copy link
Owner

lervag commented Feb 19, 2021

Ah, I see. I think this is because the behaviour is not what you expect. If you take the same file and do gg=G, you get

\documentclass{scrreprt}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
	\node at (0, 0) {
			\includegraphics{logo.png}
		};
\end{tikzpicture}

And this is actually what I expect. The point is that there is special indentation for TikZ statements, and { also adds indentation, so the rule now adds an extra indent. The following is also expected if you add some more line breaks:

\documentclass{scrreprt}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
	\node
		at (0, 0) {
			\includegraphics{logo.png}
		};
\end{tikzpicture}

@IngoMeyer441
Copy link
Author

IngoMeyer441 commented Feb 22, 2021

Ah, now I understand, thanks for the explanation. For examples like

\documentclass{scrreprt}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
    \node 
        at (0, 0) {
            \includegraphics{logo.png}
        };
\end{tikzpicture}

or

\documentclass{scrreprt}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
    \node at (0, 0)
        {
            \includegraphics{logo.png}
        };
\end{tikzpicture}

this special indentation rule is clear to me. For someone who is used to C-like programming languages the style

\documentclass{scrreprt}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
    \node at (0, 0) {
            \includegraphics{logo.png}
        };
\end{tikzpicture}

is a bit unusual. Probably there is no way to avoid the extra indentation level if the opening brace is on the same line as the TikZ command?

@lervag
Copy link
Owner

lervag commented Feb 24, 2021

For someone who is used to C-like programming languages the style

\documentclass{scrreprt}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
    \node at (0, 0) {
            \includegraphics{logo.png}
        };
\end{tikzpicture}

is a bit unusual. Probably there is no way to avoid the extra indentation level if the opening brace is on the same line as the TikZ command?

I agree, this is not how I personally like things either. However, I was never able to find a robust way to make this work exactly as I wanted.

I also don't know a good way to avoid the extra indentation level. Sorry. :\

To be honest, indentation is quite hard, because we need to do some "house keeping". Some styles of indents depend on the "state" one or more lines above. The algorithm to calculate indents assumes that every previous line is correctly indented. Then it looks at the current lines and 1 or more lines above to determine the new indent.


So: I'm not sure if there is a good way to solve this issue. As a minimum, we need a good example of "wrong indent" vs "right indent". This far, I don't see any clear "wrong indent" examples. :\

@IngoMeyer441
Copy link
Author

PR #1970 is only a partial fix since it deactivates TikZ indentation completely but users can decide which style fits better.

@lervag
Copy link
Owner

lervag commented Mar 1, 2021

Thanks, #1970 is merged now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants