22

I've recently finished my first paper written in LaTeX. I've gone through the code and tried to clean up and make it more reasonable - this is quite a challenge. I don't want you to actually rearrange anything. But maybe it's possible to point out some flaws/things that could be done more efficiently and manageable? Here is my code.

\documentclass[a4paper,12pt]{article}
\usepackage[danish]{babel}
\usepackage[latin1]{inputenc}
\usepackage{lmodern}
\usepackage[T1]{fontenc}

\usepackage{tikz}
\usepackage{pgfplots}

\usepackage{graphics}
\usetikzlibrary{calc}                               
\usetikzlibrary{decorations.pathreplacing}     

\makeatletter 
\g@addto@macro
\@floatboxreset\centering
 \makeatother


\usepackage{scrpage2}
\renewcommand{\headfont}{\itshape}
\pagestyle{scrplain}
\ohead[Frederik Hestvang, ØP Hold 1]{}
\cfoot[]{}
\ofoot[\pagemark]{}

\pagestyle{scrplain}

\usepackage[md]{titlesec}

\usepackage{changepage}
\usepackage{xcolor}
\usepackage{graphicx}
\usepackage{caption}
\usepackage[font=small, justification=justified, singlelinecheck=false]{caption}
\setlength{\topmargin}{-.3in}
\setlength{\textheight}{9.5in}
\setlength{\oddsidemargin}{-4ex}
\setlength{\textwidth}{6.7in}

\title{\vspace{-5ex}Obligatorisk opgave 2, x. september 2013.}

\date{\vspace{-5ex}}
\setlength{\abovecaptionskip}{0.5ex}

\begin{document}
\renewcommand{\labelitemi}{{\tiny$\bullet$}}

\maketitle

\vspace{-7ex}

\section{Opgave 1}
\subsection{}
\textcolor{black!40}{Tegn produktionsmulighedsområdet på ugebasis for henholdsvis Robinson
og Fredag. Beskriv hvad det angiver.}
\\
\begin{figure}[ht]
\begin{tikzpicture}
\begin{axis}[
    scale only axis,
    grid=major, grid style={dotted, black!30},
    axis lines=middle,
    inner axis line style={->},
    xlabel={$Kartofler$},
    ylabel={$Fisk$},
    ytick={0,1,...,8},
    xtick={0,1,...,8},
    ymin=-0.5,
    ymax=8,
    xmin=-0.5,
    xmax=8,
]
\addplot[color=red,thick] coordinates { %ROBINSON
            (6, 0)
            (0, 6)
        };
        \addlegendentry{Robinson}
\addplot[color=blue,thick] coordinates { %FREDAG
            (3, 0)
            (0, 2)
        };
        \addlegendentry{Fredag}

        \addplot[color=black,thick, dotted] coordinates { %HÆLDNINGROB
            (2, 4)
            (3, 4)
        };
         \addplot[color=black,thick, dotted] coordinates { %HÆLDNINGROB
            (3, 4)
            (3, 3)
         };
         \addplot[color=black,thick, dotted] coordinates { %HÆLDNINGROB
            (1, 1.3)
            (2, 1.3)
        };
         \addplot[color=black,thick, dotted] coordinates { %HÆLDNINGROB
            (2, 1.3)
            (2, 0.66)
         };

\node [right] at (axis cs:  3,  3.5) {$-1$};
\node [right] at (axis cs:  2,  1) {$-\frac{2}{3}$};

\end{axis}
\end{tikzpicture}
\end{figure}


\begin{adjustwidth}{1cm}{1cm}
Produktionsmulighedsområdet angiver mængden af de mulige forbrugskombinationer en agent eller et samfund har. Altså er det her den ramme, som Robinson og Fredag har, at sammensætte deres produktion af fisk og kartofler indenfor.
\end{adjustwidth}

\subsection{}
\textcolor{black!40}{Definér begrebet absolutte fordel og argumentér for hvem der har den
absolutte fordel i produktionen af henholdsvis fisk og kartofler?}

\begin{adjustwidth}{1cm}{1cm}
En producent siges at have absolut fordel i produktion af et gode, hvis denne har en højere produktivitet (produktion pr. tidsenhed) end den anden producent.
    Robinson har den absolutte fordel for begge goder, da hans produktivitet for begge varer er højere end Fredags.
\end{adjustwidth}

\pagebreak


\subsection{}
\textcolor{black!40}{Definér begrebet alternativomkostninger og argumentér for hvilken
sammenhæng der er mellem produktionsmulighedsområdet (her menes vel transformationskurven?) og begrebet alternativomkostninger.}
\begin{adjustwidth}{1cm}{1cm}
Alternativomkostningen ved en handling er værdien af det bedste alternativ, som man må afgive for at gennemføre en handling.
Hældningen på transformationenskurven angiver alternativomkostningen målt i et gode, ved at producere en enhed mere af et andet gode.
\end{adjustwidth}

\subsection{}
\textcolor{black!40}{Definér begrebet komparativ fordel og argumentér for hvem der har
den komparative fordel i produktionen af henholdsvis fisk og kartofler. Hvordan
bør Robinson og Fredag specialisere sig ifølge Ricardos teori om komparative
fordele, hvis de skal opnå en gevinst ved at handle med hinanden?}
\begin{adjustwidth}{1cm}{1cm}
En producent siges at have komparativ fordel hvis denne har den laveste alternativomkostning ved produktionen af et gode.
\\
Robinsons alternativomkostninger er ens for både fisk og kartofler. Fredags alternativomkostning ved at producere 1 kilo kartofler mere er $\frac{2}{3}$ kilo kartofler, og dermed $\frac{3}{2}$ kilo fisk ved at producere 1 kilo kartofler mere (fordi alternativomkostningen mellem de to varer er reciprok). Altså har...
\begin{itemize}
  \item \textbf{Robsinson den komparativ fordel i produktion af fisk: 1 kg/kartoffel}
  \item \textbf{Fredag  komparative fordel i produktion af kartofler: $\frac{2}{3}$ kg/fisk}
\end{itemize}

\vspace{1ex}

\noindent Da det ikke er givet, om Robinson eller Fredag har nogle forbrugspræferencer, skal de specialsere sig fuldt, i den vare de har størst komparativ fordel i. 
Når de handler, skal det være i intervallet mellem alternativomkostningerne, altså [$\frac{2}{3}$;1].
\\
De kan f.eks. handle 1 kg
\\
Robinson: 3 kg fisk giver 4 kg kartoffel.
\\
Fredag: 2 kg kartoffel giver 1.5 kg fisk


\begin{figure}[ht]
\begin{tikzpicture}
\begin{axis}[
    scale only axis,
    grid=major, grid style={dotted, black!30},
    axis lines=middle,
    inner axis line style={->},
    xlabel={$Kartofler$},
    ylabel={$Fisk$},
    ytick={0,1,...,8},
    xtick={0,1,...,8},
    ymin=-0.5,
    ymax=8,
    xmin=-0.5,
    xmax=8,
]
\addplot[color=red,thick] coordinates { %ROBINSON
            (6, 0)
            (0, 6)
        };
        \addlegendentry{Robinson}
\addplot[color=blue,thick] coordinates { %FREDAG
            (3, 0)
            (0, 2)
        };
        \addlegendentry{Fredag}

        \addplot[color=blue,thick, mark=*] coordinates { %FREDAG
            (1.5, 1.5)

        };
 \addplot[color=red,thick, mark=*] coordinates { %ROBINSON
            (4, 3)

        };

\end{axis}
\end{tikzpicture}
\end{figure}

\end{adjustwidth}

\end{document}
  • 3
    Welcome to TeX.SX. One thing you can do is to load only pgfplots: as it loads itself tikz which in turn it loads graphicx, the calls \usepackage{tikz}, \usepackage{graphics} can be avoided. – Claudio Fiandrino Sep 25 '13 at 7:48
  • no one else seems to have mentioned this yet. you have \usepackage{caption} twice, the first time "bare", and the second, with options. once it's read in, you lose the ability to set options by specifying \usepackage again. so omit the "bare" instance, or reset the optional elements some other way after loading. – barbara beeton Sep 25 '13 at 13:30
23

Ideally in the body of a latex document you should have no font or spacing commands, the document should be marking up the document structure with the styling of that structure specified elsewhere. This makes the document far more usable and easy to reformat if you change your mind about the formatting, or send it to a journal and they need to format it to the journal style or if you want to convert to html, or basically any use at all.

Sometimes the ideal is not possible, and for final typeset versions you need to assert control over the system and insert forced spaces and page breaks to get the best quality. That is life and is unavoidable, but such commands should be clearly marked and left to final versions so they can easily be undefined or removed if the document is re-purposed.

Your document isn't so bad (I've seen $$ title $$ used to get centred chapter titles :( but since you ask....

  \title{\vspace{-5ex}O...

No:-) The space around title is set in the class, you should be able to redefine that (or just redefine \title in the preamble) without putting space in the title (which for example in some classes would end up in the/title field in the pdf metadata and other places).

\date{\vspace{-5ex}}

As above.

\maketitle
\vspace{-7ex}

As above.

\subsection{}
\textcolor{black!40}{Tegn produktionsmulighedsområdet på ugebasis for henholdsvis Robinson
og Fredag. Beskriv hvad det angiver.}
\\

Never use \\ at the end of a paragraph, just use a blank line, also explicit colour like explicit font changes is usually a sign that something is wrong. Most likely the markup here should be

\subsection{Tegn produktionsmulighedsområdet på ugebasis for henholdsvis Robinson
og Fredag. Beskriv hvad det angiver.}

With a class/preamble definition of \subsection that sets its title in an out-dented grey paragraph.

\begin{figure}[ht]

The main effect of [ht] is to disallow p which prevents float pages, which massively increases the chance that this float may not be positioned in which case all floats will go to the end of the document. You should almost always include p if you use the optional argument at all.

\begin{adjustwidth}{1cm}{1cm}

adjustwidth can be a useful package but the environment shouldn't be used explicitly just used in the definition of a markup with more meaningful document semantics. In fact adjustwidth (like most LaTeX display environments) is just a one-item list with no label but the indented body. Here you in fact have a list; this indented text is the body with your grey text being the heading or label, so the markup should be using \subsection as suggested above or a list and using \item, but either way the relative margins between the number, the grey heading and the indented body should be defined in one place in the top of the document not in every instance.

\end{itemize}

\vspace{1ex}

\noindent  Da det ikke

That isn't an unindented paragraph, it is a continuation of the paragraph from before the list. LaTeX goes to a lot of effort to support in-paragraph lists but you have to help it by not adding blank lines which denote end-of-paragraph.

\end{itemize}
Da det

would not be indented.

17
  1. In the long run, it would be better to use UTF8 as the input encoding (along with a suitable setting in your editor).
  2. Don't mess with margins using \setlength, unless you really know what you are going. You are much better of using the geometry package
  3. Never, never ever use \\ in the text, in 99.9% of all cases a forced line break is not what you want in the text.
  4. I'd use \newpage instead og \pagebreak. The later stretches the page.
  5. The use of \vspace in the text is also a clear sign of »you are doing it wrong«
  6. What is up with all those adjustwidth's?
  • Thank you. Wrong-doing is exactly what I'm trying to avoid. 3. How would you separate the different sections/subsections if not \\ and \vspace? 6. I think adjustwidth could be avoided as well, if I knew how generalize a setting in the preamble? – Frederik Sep 25 '13 at 8:44
  • "3. How would you separate the different sections/subsections if not \\ and \vspace?" The different sections are seperated by default. However, you can try to do changes with that, see stackoverflow.com/questions/1321721/… – Dohn Joe Sep 25 '13 at 8:50
  • let LaTeX do the vertical spacing. That looks better. For a new paragraph, you just leave a blank line. For a listing, you use itemize or enumerate. One more thing I found. You are loading caption twice. – LaRiFaRi Sep 25 '13 at 8:51
  • I've tried the standard a4 predefinition in the geometry package, but it seems to me, that is fills out a rather small area (too small?) of the page? – Frederik Sep 25 '13 at 8:57
  • 2
    @Frederik Lines should be short. Having long lines means eyes have to move more and this makes reading *harder*(e.g. your eyes get tired earlier). – Bakuriu Sep 25 '13 at 9:27
15

Some hints.

  1. Use as less explicit markup as possible in the document; explicit lengths such as 1cm should be avoided.

  2. Identify how the document is structured and define environments or commands for printing those logical structures.

  3. Be as abstract as possible; for instance, don't hardwire the student's name in the specification for the header line, but define a \student command for this.

  4. Use high level packages instead of doing things by hand. For instance, use geometry for setting the page parameters rather than playing with the internals.

  5. Your environments can be defined using standard commands or by hand; using standard commands such as \subsection is not a sin, of course; by hiding it in a command (\assignment) you are free to change it as you wish, if the result is not what you find best.

  6. It's not a sin using standard environments such as itemize, either. But whenever you start adding formatting instructions to a standard environment, it's time to create your own. This is the case for the bitemize environment, which prints the items in boldface type.

After the preliminaries, here's a proposal. Note that the changes to the page parameters made useless your \pagebreak command, which in any case should be possibly added only when the document is in final form and you want to correct bad breaks. Always mark this kind of commands with an easily searchable comment, so that you can remove (or comment out) them when reediting the document.

\documentclass[a4paper,12pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc} % or latin1; match the document encoding
\usepackage[danish]{babel}
\usepackage{lmodern}

\usepackage{graphicx}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\usetikzlibrary{calc}                               
\usetikzlibrary{decorations.pathreplacing}     

\usepackage{scrpage2}

\usepackage[md]{titlesec}

\usepackage{changepage}
\usepackage{xcolor}

\pagestyle{scrplain}
\makeatletter
\ohead[\@student]{}
\makeatother
\cfoot[]{}
\ofoot[\pagemark]{}
\renewcommand{\headfont}{\itshape}

\usepackage{geometry}
\geometry{
  margin=2cm,
  heightrounded,
  headheight=18pt,
  headsep=12pt,
}

\renewcommand{\labelitemi}{\raisebox{.4ex}{\tiny$\bullet$}}

\makeatletter
\renewcommand{\@maketitle}{%
  {\centering\Large\@title\par}%
  \vspace{12pt}%
}
\newcommand{\student}[1]{\gdef\@student{#1}}
\providecommand\@student{No name given}
\makeatother

\newcommand{\assignment}[1]{%
  \subsection{}%
  {\leavevmode\color{black!40}#1\par}%
}

\newenvironment{image}
 {\begin{center}}
 {\end{center}}

\newenvironment{solution}
 {\begin{adjustwidth}{1cm}{1cm}}
 {\end{adjustwidth}}

\newenvironment{bitemize}
 {\bfseries\begin{itemize}}
 {\end{itemize}}

\begin{document}

\student{Frederik Hestvang, ØP Hold 1}
\title{Obligatorisk opgave 2, x. september 2013.}
\maketitle

\section{Opgave 1}
\assignment{Tegn produktionsmulighedsområdet på ugebasis for henholdsvis Robinson
og Fredag. Beskriv hvad det angiver.}

\begin{image}
\begin{tikzpicture}
\begin{axis}[
    scale only axis,
    grid=major, grid style={dotted, black!30},
    axis lines=middle,
    inner axis line style={->},
    xlabel={Kartofler},
    ylabel={Fisk},
    ytick={0,1,...,8},
    xtick={0,1,...,8},
    ymin=-0.5,
    ymax=8,
    xmin=-0.5,
    xmax=8,
]
\addplot[color=red,thick] coordinates { %ROBINSON
            (6, 0)
            (0, 6)
        };
        \addlegendentry{Robinson}
\addplot[color=blue,thick] coordinates { %FREDAG
            (3, 0)
            (0, 2)
        };
        \addlegendentry{Fredag}

        \addplot[color=black,thick, dotted] coordinates { %HÆLDNINGROB
            (2, 4)
            (3, 4)
        };
         \addplot[color=black,thick, dotted] coordinates { %HÆLDNINGROB
            (3, 4)
            (3, 3)
         };
         \addplot[color=black,thick, dotted] coordinates { %HÆLDNINGROB
            (1, 1.3)
            (2, 1.3)
        };
         \addplot[color=black,thick, dotted] coordinates { %HÆLDNINGROB
            (2, 1.3)
            (2, 0.66)
         };

\node [right] at (axis cs:  3,  3.5) {$-1$};
\node [right] at (axis cs:  2,  1) {$-\frac{2}{3}$};

\end{axis}
\end{tikzpicture}
\end{image}


\begin{solution}
Produktionsmulighedsområdet angiver mængden af de mulige forbrugskombinationer en agent 
eller et samfund har. Altså er det her den ramme, som Robinson og Fredag har, at sammensætte 
deres produktion af fisk og kartofler indenfor.
\end{solution}

\assignment{Definér begrebet absolutte fordel og argumentér for hvem der har den
absolutte fordel i produktionen af henholdsvis fisk og kartofler?}

\begin{solution}
En producent siges at have absolut fordel i produktion af et gode, hvis denne har en højere 
produktivitet (produktion pr. tidsenhed) end den anden producent.

Robinson har den absolutte fordel for begge goder, da hans produktivitet for begge varer er 
højere end Fredags.
\end{solution}

%%% Final adjustment
%\pagebreak



\assignment{Definér begrebet alternativomkostninger og argumentér for hvilken sammenhæng der 
er mellem produktionsmulighedsområdet (her menes vel transformationskurven?) og begrebet 
alternativomkostninger.}

\begin{solution}
Alternativomkostningen ved en handling er værdien af det bedste alternativ, som man må 
afgive for at gennemføre en handling. Hældningen på transformationenskurven angiver 
alternativomkostningen målt i et gode, ved at producere en enhed mere af et andet gode.
\end{solution}

\assignment{Definér begrebet komparativ fordel og argumentér for hvem der har
den komparative fordel i produktionen af henholdsvis fisk og kartofler. Hvordan
bør Robinson og Fredag specialisere sig ifølge Ricardos teori om komparative
fordele, hvis de skal opnå en gevinst ved at handle med hinanden?}

\begin{solution}
En producent siges at have komparativ fordel hvis denne har den laveste alternativomkostning 
ved produktionen af et gode.

Robinsons alternativomkostninger er ens for både fisk og kartofler. Fredags 
alternativomkostning ved at producere 1 kilo kartofler mere er $\frac{2}{3}$ kilo kartofler, 
og dermed $\frac{3}{2}$ kilo fisk ved at producere 1 kilo kartofler mere (fordi 
alternativomkostningen mellem de to varer er reciprok). Altså har\dots
\begin{bitemize}
  \item Robsinson den komparativ fordel i produktion af fisk: 1\,kg/kartoffel
  \item Fredag  komparative fordel i produktion af kartofler: $\frac{2}{3}$\,kg/fisk
\end{bitemize}
Da det ikke er givet, om Robinson eller Fredag har nogle forbrugspræferencer, skal de 
specialsere sig fuldt, i den vare de har størst komparativ fordel i. Når de handler, skal 
det være i intervallet mellem alternativomkostningerne, altså [$\frac{2}{3}$;1].
\begin{itemize}
\item De kan f.eks\@. handle 1\,kg
\item Robinson: 3\,kg fisk giver 4\,kg kartoffel.
\item Fredag: 2\,kg kartoffel giver 1.5\,kg fisk
\end{itemize}

\begin{image}
\begin{tikzpicture}
\begin{axis}[
    scale only axis,
    grid=major, grid style={dotted, black!30},
    axis lines=middle,
    inner axis line style={->},
    xlabel={Kartofler},
    ylabel={Fisk},
    ytick={0,1,...,8},
    xtick={0,1,...,8},
    ymin=-0.5,
    ymax=8,
    xmin=-0.5,
    xmax=8,
]
\addplot[color=red,thick] coordinates { %ROBINSON
            (6, 0)
            (0, 6)
        };
        \addlegendentry{Robinson}
\addplot[color=blue,thick] coordinates { %FREDAG
            (3, 0)
            (0, 2)
        };
        \addlegendentry{Fredag}

        \addplot[color=blue,thick, mark=*] coordinates { %FREDAG
            (1.5, 1.5)

        };
 \addplot[color=red,thick, mark=*] coordinates { %ROBINSON
            (4, 3)

        };

\end{axis}
\end{tikzpicture}
\end{image}

\end{solution}

\end{document}
  • Wow. Thats amazing. I think my biggest problem is how the syntax works inside the \makeatletter and \newcommand? It seems that makeatletter is a for-all-purposes-command? Thanks a lot, it was just what i needed; to actually see a slimmer version of my document. :) Why did you change from picture to image environment? – Frederik Sep 26 '13 at 8:21
  • @Frederik figure is for "floating figures", but you don't want them to float. In any case, rolling back would be just a matter of changing the definition of the image environment. – egreg Sep 26 '13 at 8:51
5

You must first define what “efficient” and “manageable” could mean and the benefits that you expect from a code cleanup. Consider the following scenarios:

  1. You are a UNIX hacker and want to write programs to do things like “go through file A, B and C and prepare a digest containing all sections where it goes about produktion af kartofler ”.

  2. You are going to produce a large amount of documents similar to the one you posted.

  3. You or another person of your organisation is likely to update your file in the lifecycle of your documents.

All of these steps are made easier if the description of your document resembles what you would produce with a markup language. TeX is not a markup language but a typesetting language and there are a few areas where it is not possible to ignore this:

  1. The typesetting of math formulas, where you play with struts, invisible elements and other tricks, to get the right alignments.

  2. The preparation of tables, where there is not such a thing as a “one fits them all” easy solution, but rather a comprehensive toolkit. (Look at all these tabularXY packages!)

  3. The preparation of hard paragraphs, which will not be breaked correctly by TeX without a little help.

In the remaining areas, you can almost pretend that LaTeX/TeX is indeed a markup language. Transforming your source so that it looks like a markup language file is achieved by defining macros containing typographic indications. Good candidates of snippets that you could consider promoting to a macro or an environment are:

\subsection{}
\textcolor{black!40}{…}

\begin{adjustwidth}{1cm}{1cm}
…
\end{adjustwidth}

You might also define a custom maketitle macro containing your \lineskip. It is preferrable to define macros in the preamble (before the \begin{document}) Other steps increasing clarity could be pushing definitions and package loading in included files. I would also put the picture in a separate file, because it is distracting when editing the source.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.