36

What do I mean by Convention?

First of all, allow me to clear myself about the word 'conventions.

As most of you already know, coding conventions are a set of guidelines for a specific programming language that recommend programming style, practices and methods for each aspect of a piece program written in this language [from WikipediA]. These conventions usually cover 'file organization', 'indentation', 'comments', 'declarations', 'statements', 'white space', 'naming conventions', 'programming practices', 'programming principles', 'programming rules of thumb', etc.

An Example!

As an example, consider Java (and other C-like programming languages). In Java Naming Conventions, class names should be in UpperCamelCase, methods and variables should follow lowerCamelCase and constants should be ALLCAPS.

A Personal Experience:

There are some of these conventions that I have realised while writing smaller reports. For example, the second time I wrote a report with LaTeX, I realized it's better to put each chapter in a separate file. Also, I learned about them reading other peoples code like this one that I learned about it recently: One-Sentence-per-Line. This looks very important for sake of readability of the latex source but I never thought about it until I read that answer.

Here comes the Question!

I'm about to write my master thesis and I want to know these conventions for LaTeX. My goal is to achieve a readable, reusable, easy to modify and standard source code.

If you know any conventions, or if you have your own personal ones, regarding any aspect of LaTeX, please share with me and I would be delighted if you support your answers/conventions with reasons or examples.

8
  • 1
    To make a tex code readable have a look to question Pretty printing of LaTeX code.
    – Mensch
    Jan 28, 2013 at 13:23
  • 2
  • 4
    If what you're doing is writing a document then IMO this is not worth worrying about. Coding conventions would become a real issue if you were writing a TeX package. Just separate form content. The .tex file for your thesis should only contain the text of your thesis. If you're defining commands, etc., that should go in a separate .sty or .cls file.
    – user6853
    Jan 28, 2013 at 16:23
  • 2
    There is nothing like Kernighan-Ritchie's C standard or python's PEP8 for LaTeX. Fortunately it is not as important, for good examples just look around for highly rated answers here on this site. Some elements such as the one-sentence-per-line guideline are helpful for version control tools and latexdiff.
    – Alexander
    Jan 28, 2013 at 16:28
  • 1

2 Answers 2

20

I'm about to write my master thesis and I want to know these conventions for latex. My goal is to achieve a readable, reusable, easy to modify and standard source code.

I am afraid I cannot offer any advice on the "easy to modify" and "standard source code" parts as they are contradicting the very essence of TeX and LaTeX2e. Your question sounds as if you have been influenced by some High Priest of Coding. Beware as their favorites, code and teachings change.

First let us distinguish between writing your thesis and programming LaTeX macros. I have a friend who is a painter and is one of the most untidy persons on the universe, but out of his workshop come out the most beautiful paintings. My late Supervisor's office and his desk was buried in books, papers and even old pizza boxes, but he wrote beautiful maths. Both the Professor and the artist had exceptional sharp and focus minds. Your purpose I guess is to write your thesis and get your Masters.

Organize yourself with a good distribution, set up a git or other version control system and perhaps a dropbox account, I recommend you organize your folders as follows (works for me),

    mythesis |
             - chapter01
                   - notes
             - chapter02
             - chapter-nn
             - graphics
             - plots
                  - data
             - any specials
             - delete
             bibliography files etc
             main


   mythesis-versions
             thesis-01.zip
             thesis-02.zip
             thesis-03.zip

Split the write-up from the coding. When I am writing, I don't want any distractions. It doesn't matter if the code is beautiful or not, if the text is ungrammatical or has spelling mistakes. I write quickly and focus on conveying my ideas or structuring code to work. This way I achieve "flow" much more easier. I am at my best when I do this on paper than computer. Find what works for you. In less productive hours I tidy up the code or do rewrites.

To make your code re-usable both for your older self as well as colleagues, means you need to package it and document it using literate programming, i.e, create a .dtx.

If you use re going to use LaTeX3 follow the conventions recommended by the LaTeX3 Team, is the closest thing to a standard. However, if you find yourself dealing with mostly LaTeX2e code, the following are some suggestions:

Use a suffix1 to namespace rather that a prefix in internal macros. It makes LaTeX2e code more readable, i.e.,

   myfancysection@mex

rather than

   @mex@my@fancy@section

LaTeX3 code cannot be very readable by design so stick with conventions on prefixing etc.

   g_mex_my_fancy_section

Normally author interfaces have the convention of UpperCamelCasing, break the rule if possible. It is more difficult to type

   MyFancySection

rather

   myfancysection

and I personally find the latter as readable if not easier to read than the former. The Germans do this with their language all the time.

I would make an exception and any settings commands can be uppercased.

 DeclareDocumentFont and not declareDocumentFont or declaredocumentfont

Consistency is good for example the biblatex author created commands that are easy to remember and indicate their purpose well.

  textcite[][]{}
  Textcite

Took me about an hour to find out what was wrong with:

  setpgfkeys

where I should have typed

  setpgfqkeys

I couldn't see the difference. Choose your naming scheme carefully as it is a User Interface. Use a key value interface where possible.

Just to throw the spanner in the works, the most clean and readable code, which it almost seems as if it was organized with beauty in mind is that of ConTeXt. Unfortunately I don't use it that much.

There are a lot of QA on this site around the topic, search around and pick what you think will work for you.

NOTE:

[1] On suffixes: (it also appears that natural language evolution tends to add suffixes. "der Kommunis mus" (communism); "der Naturalis mus" (naturalism); "der Touris mus" (tourism). I guess one's background in coding and mother language impact on readability opinions. As noted earlier all are personal opinions. Perhaps a linguist can shed a bit more light here.

14
  • 4
    Many of your advice is very subjective: “Use a suffix to namespace rather that a prefix in internal macros. It makes LaTeX2e code more readable” - I use the same argument in favor of a prefix! “LaTeX3 code cannot be very readable” - I find the opposite to be quite true and it's part of what I like about expl3. “[...] The Germans do this with their language all the time.” - I as German find that quite annoying as it makes text so much harder to read!
    – cgnieder
    Jan 28, 2013 at 18:56
  • @cgnieder No offence meant. I did mention a couple of times "personally find the latter as readable..' meaning compounding words without the need to CamelCase like combining words in German. Not my opinion alone guardian.co.uk/world/2012/sep/21/germans-word-long-language. I find starting a macro name with a noun or verb is also more intuitive (getproperties@myx reads better for me than myx@getproperties. Why don't you post a counter-opinion answer? Jan 28, 2013 at 19:13
  • None taken :) ... and it seems I misunderstood your point about German at first: I thought of the annoying habit of some Germans writing everything in lowercase. True: we can build arbitrarily long compound words... but at some point they also become quite unreadable and unusable while still grammatically correct. Maybe I will come up with an answer of my own...
    – cgnieder
    Jan 28, 2013 at 19:38
  • @cgnieder ... as to the German habit Barbara Beeton does it as well but for a reason:) Jan 28, 2013 at 19:41
  • 3
    @YiannisLazarides, I AM influenced by my programming background and I always have this problem of coding obsession. I really really like latex and for me, it's not only a typesetting tool but more a fun. But despite of my passion for latex, I found latex source codes very difficult to read and understand for amateurs like me. This was the whole point of my question. Let me wait for more possible answers to see other people opinions and then I will accept yours or other answers. But thanks a lot for sharing your experience.
    – Pouya
    Jan 29, 2013 at 10:49
5

I would add an important remark concerning name conventions: In your code, always use \newcommand or other high-level macro to define a new command. The reason is that you cannot redefine an existing macro of some other package.

If you need to redefine it and you know what you are doing, you can use \renewcommand or you can say \let\mywhatevernamingconventioniprefer\relax (which makes macro "undefined" in some sense). This "relaxing" is necessary if you define new macro by other commands like \DeclareMathOperator or \DeclarePairedDelimiter.

1
  • 2
    Except when this won't work, of course ;).
    – cfr
    Nov 20, 2014 at 22:53

You must log in to answer this question.

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