Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Markstat - pdf headers and syntax highlighting

    Hi all,

    Coming from an R/python background, I have been enjoying playing with German Rodriguez excellent markstat program (along with texdoc and webdoc), but I had a few questions that I haven't been able to find the answer to yet. When I convert to the .stmd to .pdf, the headers do not save as bookmarks. Is this something that can be changed? I'm not sure if this is possible, but is there also a way to include syntax highlighting like R Markdown does? I assume not as I'm guessing this is a product of being able to knit an .Rmd to .md before pandoc is used, but thought I'd ask just in case.

    Thanks

  • #2
    Callum Arnold
    I’ve not used the program previously, but for the syntax highlighting you would likely need to include either a javascript function or CSS file that would identify the words to highlight and how to highlight them. There are a few people who have posted solutions for syntax highlighting on GitHub in the context of HTML and SublimeText. Might be a place to start?

    Comment


    • #3
      Thanks wbuchanan. I've looked up a few of them and a few seem to do the trick for pdfs.

      Comment


      • #4
        Callum Arnold I am glad you have enjoyed playing with -markstat-.

        In terms of bookmarks, I find that if you generate pdf via LaTeX the output will include bookmarks. I just opened the PDF version of the markstat manuscript (http://data.princeton.edu/stata/markdown/markstat.pdf) in Adobe Reader (not the native reader in Chrome) and it does show bookmarks for all eleven sections. This is all courtesy of Pandoc. When you define a section or subsection you can add {#bookmark} after the title, but if you don't Pandoc will generate a unique id for you. There is an issue if your bookmark includes symbols because PDF bookmarks are restricted to plain text. This is discussed (with a workaround) at https://github.com/jgm/pandoc/issues/3555.

        As for syntax highlighting, this is not supported in -markstat- at the moment, but it is something that might be added in the future. The -markstat- command splits the input file into Makdown, Stata and R code, and uses Pandoc just for the Markdown code, handling Stata and R code directly. It is possible to split the Stata output into commands and results, and then apply syntax highlighting to the commands. A separate issue is syntax highlighting in your editor, which would of course depend on which editor you use. Mark Masnick has a post on using -markstat- with sublime, see https://maxmasnick.com/2017/09/22/markstat/

        Comment


        • #5
          there is a Stata package that supports syntax highlighting in HTML and LaTeX. The package is called statax and is hosted on GitHub.

          It is also integrated in MarkDoc (it was actually developed as a part of MarkDoc), so your HTML, LaTeX, and PDF documents can automatically highlight Stata code. But it is fairly simple to add it to an HTML or LaTeX document.
          ——————————————
          E. F. Haghish, IMBI, University of Freiburg
          [email protected]
          http://www.haghish.com/

          Comment


          • #6
            Thanks German Rodriguez . I have been using LaTeX to generate the pdfs, and viewing in Adobe Reader, and whilst I can see them for the markstat documentation it is not showing for my documents (please see code and output below). Regarding the IDE, I'm using Atom + hydrogen stata kernel + stata-language, so the syntax highlighting is sorted unless I use strict code blocks.

            Code:
            ---
            title: WB07 Practical
            author: Callum Arnold
            date: 18 December 2018
            ---
            
            # Learning{#bookmark}
            Click image for larger version

Name:	markstat_bookmarks.PNG
Views:	1
Size:	21.4 KB
ID:	1475496


            And thanks haghish that looks very interesting. I'll see if I can get it to work as it seems to do what I want in terms of syntax highlighting etc.

            Comment


            • #7
              Apologies German Rodriguez, but I had one further question. It appears that the order in which graphs are stitched into the pdf doesn't match up with the code. I have the code

              Code:
                  histogram bweight, bin(12) start(0) fraction normal
                  graph export hist1.png, width(500) replace
              
              ![Birthweight in grams](hist1.png)
              
              Now let's compute the CIs for this distribution. Note that STATA automatically uses the t-distribution, not normal.
              And I get the following output in the pdf.

              Click image for larger version

Name:	markstat_order.png
Views:	1
Size:	41.4 KB
ID:	1475635

              Is there an obvious mistake that I'm making to cause the images to be stitched in the wrong place, as I can't see how it's different from the example in the markstat manuscript, but it's a recurring issue in my document?

              Thanks,

              Comment


              • #8
                Hi Callum Arnold . A couple of answers, I hope they work for you.

                1. Bookmarks. Turns out one needs to run pdflatex twice to get the bookmarks, but there is a simpler solution via the -bookmark- package. Modify the metadata on your example by adding a header-include, so the file looks like this

                Code:
                ---
                title: WB07 Practical
                author: Callum Arnold
                date: 18 December 2018
                header-includes:
                 - \usepackage{bookmark}
                ---
                
                # Learning{#bookmark}
                Give it a try. (One can also run pdflatex twice if you add the markstat option -keep(tex)- to keep the latex file and then run pdflatex on a command window, but I found that using the package is easier.)

                2. Figures. I think this is just a LaTeX issue, which is famous (infamous?) for deciding where to place float environments such as figures. One thing you could try is making the image a bit smaller, which increases the chances that it will be placed "right here". You can do this by coding the width as a percentage of the page width (minus the margins):

                Code:
                ![Birthweight in grams](hist1.png){width=60%}
                The default in -markstat- is 75% for documents and 65% for beamer slides. An alternative is to use the -float- package, which you can add using a header include as above, but then you need to include the figure using LaTeX; you will find an example at https://tex.stackexchange.com/questi...cement-in-text. Another approach is to modify the constraints that LaTeX uses in placing figures, for a nice discussion see https://robjhyndman.com/hyndsight/latex-floats/.

                Hope this helps.

                Comment


                • #9
                  Hi German Rodriguez, that worked perfectly. I had one quick follow up question to using float within the file. I loaded float using the header, and then used the following code as suggested in the links you provided.

                  Code:
                  ![Birthweight in grams](hist1.png)[H]
                  This placed the image exactly where I wanted (the 'here' bit), but it also included [H] in the pdf. I'm afraid that my LaTeX knowledge isn't very advanced, so I wasn't sure if there was a way to avoid this behaviour?

                  Thanks for all your help - I really appreciate it!

                  Comment


                  • #10
                    Hi Callum Arnold. Markdown doesn't understand the [H] because that is a LaTeX argument, so it passes it along and ends up in the output.

                    Let me note first that if the figure was placed exactly where you wanted it you don't need the [H], so just code

                    Code:
                    ![Birthweight in grams](hist1.png)
                    If that doesn't produce the result you want, first make sure the metadata includes the float and graphicx packages

                    Code:
                    ---
                    header-includes:
                     - \usepackage{float}
                     - \usepackage{graphicx}
                    ---
                    Then include the figure using LaTeX code with the [H] argument:

                    Code:
                    \begin{figure}[H]
                    \centering
                    \includegraphics[width=0.75\linewidth]{hist1.png}
                    \caption{Birthweight in grams}
                    \end{figure}
                    Even then there is no guarantee that LaTeX will put the figure right there. I usually get better results tinkering with the width.

                    BTW this shows how simple Markdown is, as the one-liner in the top code block generates exactly the five lines of LaTeX code in the bottom block, minus the [H].

                    Comment


                    • #11
                      Thanks German Rodriguez - very helpful! And yes, it does highlight the advantages of Markdown.

                      Comment

                      Working...
                      X