# How to center an image?

Is it possible to show the image below centered instead of aligned to the left?

If You Really Must...

The way I center an image is to put an appropriately sized \hspace{} before the image. In this answer, you can see

Here is a plot of $\displaystyle\frac1{\sqrt{n}}+\frac{(-1)^n}n$ showing the non-monotonicity:

$\hspace{3cm}$![enter image description here](http://i.stack.imgur.com/d5eu5.png)

which shows up as

Here is a plot of $\displaystyle\frac1{\sqrt{n}}+\frac{(-1)^n}n$ showing the non-monotonicity:

$\hspace{3cm}$

Note that this only works in those forums that support MathJax (such as math.stackexchange.com).

As mentioned in a comment to the question, there is this post by Zev Chonoles that suggests this method and HTML markup, using multiple &nbsp;s or spaces in a <pre>...</pre> block.

However, whether MathJax and \hspace{} or HTML and &nbsp;, these are both workarounds for a real centering method from the developers. In terms of editing posts if we should ever get a real centering method, using the simplest option seems the best.

Centered: To Be or Not To Be...

As Davide Cervone points out in comments, it is not friendly to the visually impaired to indent images using MathJax. Some readers may add a confusing MathMode interlude where there is only whitespace (one might say that a good reader would skip these, but I imagine readers might lack this feature just as SE lacks image centering). So in the end, this means we should not attempt to center our images and ask SE to please provide image centering.

Furthermore, we should also fill in the "enter image description here" for our images, so that the readers can describe an image for the visually impaired.

• One should not use MathJax to create "white-space." – quid Dec 8 '14 at 7:18
• @quid: thanks for your caveat. Do you have anything positive to suggest? Do you have an explanation of why, if the forum supports MathJax, one should not use it? It can't be an issue of portability, since our posts here are riddled with MathJax. It seems to me that a single, easily adjusted \hspace{} command is better than a bunch of &nbsp;s. – robjohn Dec 8 '14 at 10:18
• The main reason is that MathJax should best be used for math only, for semantic reasons. There are some other reasons too, but that one should be the clearest. Let me just reproduce the reply/confirmation I got recently from Davide Cervone, the lead developper of MathJax: "MathJax should be used only for mathematical content, not for adding white space, or italics, or colored text, or background colors, or line breaks in comments, or other display hacks, to textual (non-math) content." – quid Dec 8 '14 at 11:19
• For a positive suggestion: Have trust in the default layout; on average it is better and at least it is good enough. Or, what Najib Idrissi said, not sure if it qualifies as positive. Or, what Mariano said on the first linked post. Do you know how much 3cm are relative to my screensize? The same applies to the &nbsp; due to the brilliant "centering" I can only see half the image there! The rest is "centered" right outside my screen. Let us just stop this kind of tinkering. – quid Dec 8 '14 at 11:37
• @quid: The same could be said of any workaround used to simulate features that we don't have otherwise. MathJax is a markup language, just like HTML; so the same argument can be made against using HTML for adding whitespace. I don't like using either of them, because they are not actually centering and are relying on the width of the page. Does that mean, without centering support from the SE developers, that we should simply have badly formatted images in posts? – robjohn Dec 8 '14 at 11:38
• It seems you have not seen my second comment. Yes, we should accept that format. Also, it does not seem quite correct to say that the same as for MathJax could be said for HTML. Roughly, MathJax is for math, while HTML is for hyper text in general, as indicated by the respective names. – quid Dec 8 '14 at 11:39
• We use what we are given. If I don't have a napkin, I will use a paper towel. Even though a napkin was designed for that purpose, I don't want to do without something there. If they are not given a real centering method, people will use what they can. I doubt people will stand on semantics and settle for left-justified images. – robjohn Dec 8 '14 at 12:01
• @robjohn: I've settled for left-justified images. ;-) – user642796 Dec 8 '14 at 12:26
• I'm sorry, @robjohn, but clearly the intended purpose of MathJax is to allow the formatting of mathematical formulae, and not to provide spacing to mimic center-alignment of other elements, nor to meet minimum character requirements in text fields ${}{}{}{}{}{}{}{}{}$-style. As rendering MathJax is fairly intensive, it makes sense to consider such alternative uses misuses. – user642796 Dec 8 '14 at 14:03
• @Wood: I have never had a problem seeing the images on my iPhone in my posts using \hspace{} either. I don't think that viewing images on mobile devices is the problem; it is a semantic issue: MathJax was intended for the layout of mathematics. It is interesting that \hskip is even supported in MathJax since that was not intended for mathematical equations; \hskip is a TeX command for text layout, \mskip is the TeX command for math layout. – robjohn Dec 8 '14 at 16:26
• @ArthurFischer: I am not arguing that using MathJax for spacing other parts of a post is the right thing to do. For that matter, no workaround is really the right thing to do, but it is usually all that can be done until there is a right thing to do. If the SE developers would give us some sort of centering, whether limited to images or more general, we could do the right thing. Just for the record, I despise the use of ${}{}{}{}{}$ just to get around the minimum comment length. – robjohn Dec 8 '14 at 16:33
• Since my comments have been bandied about, here, I thought I would respond with a little clarification. First, my comment to quid was not a private one, but a public one here. Second, in terms of semantics, this is not just an abstract idea of good practice but one that actually affects people. As described in the link I just gave, one important group of users who are affected by this is those using screen readers (and that includes not just blind, but also dyslexic ... – Davide Cervone Dec 9 '14 at 15:58
• ... and other users with special needs). Having things visually correct but semantically broken will make their (already hard) experience harder. Marking something as mathematics that is really only there for spacing can cause confusion, as they will hear a math section that has not math content. My own feeling is that minor improvement in visual aesthetics is outweighed by the detrimental affect it has on the non-visual uses of the page. – Davide Cervone Dec 9 '14 at 16:01
• @robjohn, since you said the MathJax website wasn't clear about it, let me say that MathJax currently is intended only to format equations; other layout is expected to be done via HTML. (MathJax's internals allow for non-math content, but so far no component uses that.) In terms of images, Math is frequently used as labels in images, but images in equations are much rarer (though still done). MathJax does supper the <mglyph> MathML element for this, but there is no built-in means of accessing it from TeX. There is a 3rd-party extension for it, however. – Davide Cervone Dec 9 '14 at 16:59
• @quid: true, but the fewer patterns we need to search for to find images to center properly, the easier the task will be. If we suggest one or two to the community, most will use one of those suggestions. – robjohn Dec 11 '14 at 17:07