This answer focuses more on minimalizing the code, rather than finding the source of the problem, as the once top-voted answer does. It is intended to be concise and hands-on, but digestible rather than exhaustive. Suggestions for improvement are welcome!
Here are some strategies for reducing your code, which will help you get better and faster answers, since it will be clearer what your problem is and the other users will see that you put some effort into producing a concise Minimal Working Example. Thanks for that!
Most likely, not all of these things will apply to your question, so just pick what does apply. However, it is advised that you provide the community with something that will reproduce the problem in the easiest way possible. Typically this requires code that starts with \documentclass
and ends with \end{document}
(if using LaTeX). It will allow readers to copy-and-paste-and-compile your code and see exactly what problems you might be experiencing.
What follows below are snippets of code; bad references imply that it should typically not be used, as it may not be part of the problem, while good references make suggestions that should be used instead. Note that these snippets should still form part of a larger, \documentclass
...\end{document}
structure as mentioned above.
Document Class
- Bad:
\documentclass{MyUniversitysThesisClass}
- Bad:
\documentclass[..]{standalone}
...unless your problem relates to the standalone
document class. standalone
is meant for cropping stand-alone images within a main document usually. If this doesn't pertain to you, don't use it.
+ Good:
\documentclass{article}
Using a non-standard document class? Does your problem still show up with article
? Then use article
.
Document Class Options
- Bad:
\documentclass[12pt, a5paper, final, oneside, onecolumn]{article}
+ Good:
\documentclass{article}
Using any options for your document class? Does your problem still show up without them? Then get rid of them.
Comments
- Bad:
\usepackage{booktabs} % hübschere Tabllen, besseres Spacing
\usepackage{colortbl} % farbige Tabellenzellen
\usepackage{multirow} % mehrzeilige Zellen in Tabellen
\usepackage{subfloat} % Sub-Gleitumgebungen
+ Good:
\usepackage{booktabs}
\usepackage{colortbl}
\usepackage{multirow}
\usepackage{subfloat}
You put comments in your code to remember what packages are there for? Great habit, but usually not necessary in a MWE – get rid of them.
Loading Packages
- Bad:
\usepackage{a4wide}
\usepackage{amsmath, amsthm, amssymb}
\usepackage{url}
\usepackage[algoruled,vlined]{algorithm2e}
\usepackage{graphicx}
\usepackage[ngerman, american]{babel}
\usepackage{booktabs}
\usepackage{units}
\usepackage{makeidx}
\makeindex
\usepackage[usenames,dvipsnames]{color}
\usepackage{colortbl}
\usepackage{epstopdf}
\usepackage{rotating}
+ Good:
% Assuming your problem is related e.g. to the rotation of a figure, you might need:
\usepackage{rotating}
You’ve developed an awesome template with lots of helpful packages? Does your problem still show up if you remove some or even most of them? Then get rid of those that aren’t necessary for reproducing the problem. (If you should later find out that another package is complicating the situation, you can always ask another question or edit the existing question.)
In most cases, even packages like inputenc
or fontenc
are not necessary in MWEs, even though they are essential for many non-English documents in “real” documents.
Images
- Bad:
\includegraphics{graphs/dataset17b.pdf}
+ Good:
\usepackage[demo]{graphicx}
....
\includegraphics{graphs/dataset17b.pdf}
+ Good:
\usepackage{graphicx}
....
\includegraphics{example-image}% Image from the mwe package
Your problem includes an image? Does your problem show up with any image? Then use the option demo
for the package graphicx
– this way, other users who don’t have your image file won’t get an error message because of that. If you prefer an actual image that you can rotate, stretch, etc., use the mwe
package, which provides a number of dummy images, named e.g. example-image
.
If your problem is specific to the size of the included image, still use mwe
's example-image
, but also specify the width
and height
so it more readily replicates your custom-image
dimensions. Again, this way the problem is reproducible without using your image.
Text
- Bad:
In \cite{cite:0}, it is shown that $\Delta \subset {U_{\mathcal{{D}}}}$. Hence
Y. Q. Qian's characterization of conditionally uncountable elements was a
milestone in constructive algebra. Now it has long been known that there exists
an almost everywhere Clifford right-canonically pseudo-integrable, Clairaut
subset \cite{cite:0}. The groundbreaking work of J. Davis on isomorphisms was a
major advance. In future work, we plan to address questions of uniqueness as
well as degeneracy. Thus in \cite{cite:0}, the main result was the
classification of meromorphic, completely left-invariant systems.
+ Good:
\usepackage{lipsum} % just for dummy text
...
\lipsum[1-3]
+ Good:
Foo bar baz.
Need a few paragraphs of text to demonstrate your problem? Use a package that produces dummy text. Popular choices are lipsum
(plain paragraphs) and blindtext
(can produce entire documents with section titles, lists, and formulae).
Need just a tiny amount of text? Then keep it maximally simple; avoid formulae, italics, tables – anything that’s not essential to the problem. Popular choices for dummy words are foo
, bar
, and baz
.
Bibliography Files
+ Good:
\begin{filecontents*}{\jobname.bib}
@book{Knu86,
author = {Knuth, Donald E.},
year = {1986},
title = {The \TeX book},
}
\end{filecontents*}
\bibliography{\jobname} % if you’re using BibTeX
\addbibresource{\jobname.bib} % if you’re using biblatex
Need a .bib file to reproduce your problem? Use a maximally simple entry embedded in a filecontents
environment in the preamble. During the compilation, this will create a .bib file in the same directory as the .tex
file, so users compiling your code only need to save one file by themselves.
Another option for biblatex
would be to use the file biblatex-examples.bib
, which should be installed with biblatex
by default. You can find it in bibtex/bib/biblatex/
.
Data
-- Bad:
Never include data as an image.
- Bad:
Number of points Values
10 100
20 400
30 1200
40 2345
etc...
+ Good:
\begin{filecontents*}{data.txt}
Number of points, Values
10, 100
20, 400
30, 1200
40, 2345
\end{filecontents*}
Including the data as part of the MWE makes the example portable as well. Of course, the input may differ depending on what package you use to manage the data (some require CSV, some don't).
Index
+ Good:
\begin{filecontents*}{\jobname.ist}
delim_0 "\\dotfill "
\end{filecontents*}
The index style can be included in the filecontents*
environment in the preamble. The contents (and file extension) will differ according to the required indexing application (makeindex
or xindy
).
Sometimes a problem can only be demonstrated with an index that spans several pages. The testidx
package is like lipsum
etc but the dummy text is interspersed with \index
to make it easier to test index styles. It has over 400 top-level terms (along with some sub-items and sub-sub-items) that includes every basic Latin letter group (A–Z) as well some extended Latin characters and a few digraphs.
- Bad:
\begin{document}
aa\index{aa}
ab\index{ab}
...
zy\index{zy}
zz\index{zz}
\printindex
\end{document}
+ Good:
\begin{document}
\testidx
\printindex
\end{document}
If page breaking is the source of your problem (for example, after a letter group heading or between an item and sub-item), there's a high probability of an awkward break occurring given the large number of test items, but you can alter the page dimensions or font size to ensure one occurs in your MWE.
Glossaries
The glossaries
package comes with some files containing dummy entries, which can be used in MWEs.
- Bad:
\newglossaryentry{sample1}{name={sample1},description={description 1}}
...
\newglossaryentry{sample100}{name={sample100},description={description 100}}
\newacronym{ac1}{ac1}{acronym 1}
...
\newacronym{ac100}{ac100}{acronym 100}
+ Good:
\loadglsentries{example-glossaries-brief}
\loadglsentries[\acronymtype]{example-glossaries-acronym}
See Dummy Entries for Testing for a complete list of dummy entry files provided by glossaries
. There's an additional file example-glossaries-xr.tex
provided by glossaries-extra
.
Formatting your code
Formatting of code is done using Markdown. See the relevant FAQ How do I mark code blocks?. There also exists some syntax-highlighting, a discussion of which can be following at What is syntax highlighting and how does it work?.
With the above in mind, don't post your code in comments, since comments only support a limited amount of Markdown.
Posting a Picture of Your Output
It’s often helpful to see what your current, faulty output looks like. If you’re not sure how to do that, have a look at How does one add a LaTeX output to a question/answer? and how can i upload an image to be included in a question or answer?.
Selection of packages inspired by Inconsistent rotations with \sidewaysfigure. Math ramble generated by Mathgen. Bibliography sample from lockstep’s question biblatex: Putting thin spaces between initials.