13

On the main site I see a lot of edits that replace the indention by 4 spaces for code blocks with ```code block``` (for example https://tex.stackexchange.com/posts/447721/revisions).

Does this mean this syntax is to be preferred? And if yes, why does the {} button indent the code instead of adding ```?

3
  • In fact, the beginning is not just "```" but "```latex". I've never seen this before; perhaps it's a new feature, since latex is useful in more than just this site, and latex code might need to be presented differently from code for other languages. The person who edited the referenced post ie a relative newbie on tex.sx, so may have picked up this convention somewhere else. I'm inclined to continue using the {} technique, since it's provided as an option for the question and answer boxes, and is easy to describe. Commented May 19, 2019 at 14:30
  • 2
    There are two ways to format code according to the Markdown specification: indented code blocks (the 4 spaces) and fenced code blocks (the ``` syntax). Until some time ago the stackexchange network didn't support fenced code blocks. Now that they do, in the ask question window you see this, so it looks like they are encouraging it. My take at it is that both options will always be available to choose from, since both are part of the specification. Commented May 19, 2019 at 14:45
  • 3
    Now my 2 cents (which didn't fit in the comment above): I prefer the code fences for 2 main reasons (one for each cent :) 1) Changing syntax is far easier. The indented code blocks require HTML comments like <!-- language: lang-latex -->, and I never remembered if it's lang-latex or latex-lang, while now you just write the language name after the code fence. 2) I found it really annoying that whatever last-minute line break I decided to add to the code I had to put 4 spaces before it, while now the code remains as it should be. Commented May 19, 2019 at 14:51

1 Answer 1

14

I generally use four spaces, but I use ``` whenever necessary

Four spaces is very good. Actually, I do not use four spaces, but the shortcut offered by Stack Exchange (Ctrl + K). It clearly takes much less time than using ```.

However, there are two main drawback of using that I can see

  • Switching code language

    Suppose that I want to post a C code,

    #include <stdio.h>
    int main() {
        printf("Hello, world!\n");
    }
    

    I have to use a very long string

    <!-- language: lang-cpp -->
    

    but if I use the ``` syntax, I just have to use

    ```cpp
    

    Clearly the ``` syntax is much better here.

  • Listings inside a list

    Have you ever tried to post a code inside a list environment, like the above codes?

    Note that you must indent your code by eight spaces!

    * This is some text in a list environment
    
            This is some code
    

    Unfortunately, the Ctrl + K or the {} button don't work here. If I use those shortcuts for the above C code, the result will be

    include

    int main() { printf("Hello, world!\n"); }

    This is where the ``` syntax shows its greatest benefit. I just have to use the shortcut to add four spaces and add the backticks to the indented listing. Result

    This is where the <code>```</code> syntax shows its greatest benefit. I just 
    have to use the shortcut to add four spaces and add the backticks to the 
    indented listing. Result
    
        ```cpp
        #include <stdio.h>
        int main() {
            printf("Hello, world!\n");
        }
        ```
    
    #include <stdio.h>
    int main() {
        printf("Hello, world!\n");
    }
    

    Of course, with a higherlevel list, like this

    • This is some code

      \documentclass{article}
      

    it requires more spare time to manually insert spaces.

However I disagree with the linked revision in case it is from another user

The user who made the revision is the answerer himself, so any edits are very acceptable and welcome. However, please note that your suggested edit will be rejected by me in the review queue if you propose such an edit.

The following part does not criticize the answerer – he just edits his post, so nothing is wrong here. I appreciate his revision. But the part criticizes all suggested edits with this style.

Please do NOT propose useless suggested edits just for substituting four spaces with backticks!

I will reject all such edit suggests.

The linked revision has three parts:

  • Substitute four spaces with backticks: I disagree with it. What is wrong with four spaces?
  • Add > to the quote: Well, how does the output change then? Is it okay to have a multiline paragraph code with only >? Absolutely okay, so I disagree with this also.
  • According to package [wrapfig][1] to According to [wrapfig][1] documentation: Alright, this is at least acceptable, but that is not a major edit and not so necessary.

In short, if it is from a user less than 2000 rep and not the post owner, I will reject it right away.

5
  • 1
    Regarding any edits [from the answerer] are very acceptable and welcome: I don't really agree there, if an edit is just to substitute four spaces for backticks and maybe a few other small things, then the edit is trivial and it should not be done. It does not make a difference if the edit is made by the original answerer/questioner or not. The result looks the same and it puts the post back on the first page of the question list, which is undesired because then there is less space for new questions, new answers and questions/answers that are edited in a meaningful way.
    – Marijn
    Commented May 20, 2019 at 21:06
  • @Marjin He is just editing his own post!
    – user156344
    Commented May 20, 2019 at 23:50
  • 1
    Yes, but small edits that do not really improve a post should not be done, especially for older posts, it does not matter if it is your own post or not. The edits need to be reviewed (if the poster has less than 2000 reputation) and the post goes back to the front page without much changes. Of course TeX.SE is a relatively small site with a small amount of questions every day, so the extra reviewing work and the posts reappearing are not really a big problem, but still such edits add unnecessary distraction/noise to the site.
    – Marijn
    Commented May 21, 2019 at 7:16
  • 1
    Unrelated: What I've been doing for adding code in lists is to first use Ctrl + K, then select the code block and one additional line before/after the code, and use Ctrl + K again. I think I've mostly added a single letter on that additional line, and then deleted afterwards. Commented May 21, 2019 at 16:41
  • @TorbjørnT. Thanks for that! It is clearly quicker than mine. But after all, all of them are tricks ;) Hope the SE developers will add a more general code button soon...
    – user156344
    Commented May 21, 2019 at 16:45

You must log in to answer this question.