1
$\begingroup$

The only way I know how to have multiline text "show" as multiline text is to add 4-5 leading spaces to each line of pasted text.

 line 1
 line 2

This is tedious for many lines of text like code or data. Is there a mathjax command that makes each line use the carriage return within the pasted text without the leading spaces? Is there something like begin{} end{} that does what double backslashes do for math expressions but without having to add the backslashes?

$\endgroup$
8
  • 3
    $\begingroup$ Re: This is tedious for many lines of text like code or data. In fact, you can do the same thing by selecting the text and pressing Ctrl+K.. Or by clicking the corresponding button in the editor. (Rather than adding four spaces manually for each line.) All these methods are mentioned also in the tag-info for code-block on Meta Stack Exchange. (Although it's quite possible that this is something completely different from what you're trying to achieve.) $\endgroup$ Feb 19 at 19:59
  • $\begingroup$ @Martin Sleziak What I want to do in by browser window on Math Stack Exchange is be able to paste lines of text (perhaps between some pair of indicators) and have them show as separate lines of text. I want to do what I did here without having to add the leasing spaces. $\endgroup$
    – poetasis
    Feb 19 at 20:38
  • $\begingroup$ If adding leading spaces to every line is equivalent to pressing a few keys once, why do you need something else? $\endgroup$ Feb 19 at 20:49
  • $\begingroup$ @Misha Lavro I thought you were talking about a text editor or something. I tried the crtl-K thing and it added leading spaces to all the lines of text. It is a workable solution but it seems like there should be something that does not add spaces to the editing text. thanks. $\endgroup$
    – poetasis
    Feb 19 at 21:10
  • $\begingroup$ Add two spaces to the end of each line of text to force a carriage return. Using code block formatting solely to force carriage returns is not recommended, since they interact differently with screen readers, for instance. (I’m not sure whether you’re asking to format code or plain text, hence this comment…) $\endgroup$ Feb 20 at 3:08
  • 1
    $\begingroup$ and to finish @TheAmplitwist's comment; in case Mathjax is actually required, \begin{align}...\end{align}, \begin{array}...\end{array} and even $$...$$ (this last one is special to mathjax, does not work in LaTeX) supports multi-line output with two slashes \\. Text can be forced into Mathjax with \text{...} $\endgroup$ Feb 20 at 3:15
  • $\begingroup$ @Calvin Khor I know to use several \begin{}, \end{}, \text{}, $$, and other formatting “commands”. I was hoping there was one to make the typesetter to respond to the carriage returns already in the text. $\endgroup$
    – poetasis
    Feb 20 at 3:28
  • 2
    $\begingroup$ @poetasis At the risk of being too pedantic: there are three separate things working here. You can ask for linebreaks in normal text; you can do so in a codeblock; or you can force linebreaks in Mathjax. Mathjax is almost never used for the whole post. The Mathjax interpreter only starts working when it sees one of the $...$, $$...$$ or \begin{...}...\end{...} pairs. Without these things, Mathjax is not used, so it is not possible to ask Mathjax to do anything. In contrast, your example displays a code block, and does not use any Mathjax. $\endgroup$ Feb 20 at 5:20

1 Answer 1

5
$\begingroup$

As alluded to in the comments by Martin Sleziak, there are a few ways of creating code blocks:

Inline code (for code that does not contain newlines)

  • When the cursor is not in the first column, or you've selected code that does not hold newlines, click the {} code button above the editor (pictured below) or hit Ctrl+K (Cmd+K on macOS)
  • Enclose with backticks: `<html>`
  • Embed within <code> tags, and manually encode HTML entities: <code>&lt;html&gt;</code>

Blocks of code (preserves newlines)

  • When the cursor is on an empty line, or you've selected code including a newline, click the {} code button above the editor (pictured below) or hit Ctrl+K (Cmd+K on macOS)
  • Create code fences with three backticks ``` or tildes ~~~, one on a line before the code, and one on a line after
  • Indent the code by four spaces
  • Encase in <pre> or <pre><code> tags (in that order; using <code><pre> is invalid), and encode HTML entities (like &lt; for <) yourself

All of these seem to work on my system.


Regarding the {} icon:

Screenshot of answer box showing location of the code button on the editing toolbar.


I don't know that you can (or should) expect anything more. The goal here is to make it easy to input text with a fairly restricted number of (syntactic) formatting options. Code blocks are indicated by leading spaces. Those spaces can be inserted in a number of ways but, at the end of the day, they need to be there if you want a code block.

$\endgroup$
6
  • $\begingroup$ Where is the '{}' icon? What formatting bar? $\endgroup$
    – poetasis
    Feb 20 at 0:21
  • 1
    $\begingroup$ @poetasis here: i.stack.imgur.com/c2Hv7.png $\endgroup$ Feb 20 at 3:20
  • $\begingroup$ @Calvin Khor What application has that formatting bar? I want something that works on math stack exchange in a browser window. I use Chrome. $\endgroup$
    – poetasis
    Feb 20 at 3:32
  • 3
    $\begingroup$ @poetasis That is the top of the box used to write an Answer on this website. Here is a screenshot with more of the website shown. No added software. It is the same in all browsers. It is the same when writing a Question. (Sorry Xander for the pings) $\endgroup$ Feb 20 at 5:24
  • 2
    $\begingroup$ @Calvin Khor I am surprised that I never saw that “bar” before because I have a habit of ignoring things that take up valuable vertical real estate. Thank you. $\endgroup$
    – poetasis
    Feb 20 at 9:23
  • 1
    $\begingroup$ @poetasis glad to help! $\endgroup$ Feb 20 at 9:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .