Announcement

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

  • How to prevent collect from escaping Latex characters?

    Hello everyone,

    I'm exporting some regression results into Latex with etable + collect. I added a few labels as I want them to appear in the .tex file, but the result has the "$" and "_" characters escaped.

    Example:
    Code:
    sysuse auto
    reg price mpg weight
    estimates store first
    reg price mpg length
    estimates store second
    etable, estimates(first second)
    collect label levels colname mpg "$\beta_{mpg}$", modify
    collect export reg_results, name(ETable) as (tex) replace
    The resulting line in the .tex output becomes
    Code:
    \multicolumn{1}{l}{\$\beta\_{mpg}\$} &
    Which won't format correctly. Is there any way to prevent the escaping?

    I tried
    1. Escaping it in the label collect label levels colname mpg "\$\beta\_{mpg}\$", modify --- no difference
    2. Looked it up, found a question from someone using esttab, but there is no substitute option for the collect label and collect export commands.

    Best,
    Clara
    Last edited by Clara Daru; 09 Jan 2024, 20:36.

  • #2
    Thanks for the data example. This is a difficult issue. Sometimes you want the dollar sign to be a dollar sign and sometimes you want it to indicate the beginning or end of math mode. Unless you compel the user to escape all dollar signs in the text, you cannot have it both ways. So my guess is that the programmers choose the lesser of two evils. In this case, you can use the command filefilter to address your issue, assuming all dollar signs indicate the beginning or end of math mode. The same logic holds for the underscore.

    Code:
    sysuse auto, clear
    reg price mpg weight
    estimates store first
    reg price mpg length
    estimates store second
    etable, estimates(first second)
    collect label levels colname mpg "$\beta_{mpg}$", modify
    collect export reg_results, name(ETable) as (tex) replace
    filefilter "reg_results.tex" "reg_results1.tex" , from("\BS$") to("$") replace
    filefilter "reg_results1.tex" "reg_results.tex" , from("\BS_") to("_") replace
    Res.:

    Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	44.1 KB
ID:	1739333

    Comment


    • #3
      Thanks for that explanation Andrew. But latex users know to escape their dollar signs if they need to. This is a bug in collect export, plain and simple.

      Comment


      • #4
        Originally posted by Michael Smart View Post
        This is a bug in collect export, plain and simple.
        Not necessarily, in my opinion. You don't want LaTeX users to have one set of syntax and others a different one. Ideally, if I have "$20" in my output, I want it to display as such when exported and however exported. For example, it displays correctly in a DOCX format. However, in LaTeX, the dollar sign needs to be escaped. The programmers chose to handle this by escaping dollar signs by default, which makes sense. However, this becomes problematic if you are using dollar signs to indicate the start of math mode.

        This issue was recently discussed in this thread: https://www.statalist.org/forums/for...etable-command. My suggested workaround is to use

        Code:
        \( \)
        instead of

        Code:
         $ $
        to indicate math mode.

        Comment


        • #5
          also
          Code:
          \begin{math}\end{math}

          Comment

          Working...
          X