Stargazer puts an additional $...$ around the minus sign of a negative numeric value - stargazer

If I have a negative numeric value in R that I want to output into latex code using stargazer, I get something like:
$$-$ 2$
which Latex cannot handle because there is no space between the first two Dollar signs. This occurs if stargazer interprets a negative value:
library("stargazer")
stargazer(-2, summary=F) # delete the minus sign to see the difference, it'll be: $2$
How can I avoid that? Latex is completely fine with getting $- 2$ and interprets that as math so how can I either get rid of those extra dollar signs around the minus sign or, as a workaround, add a space between the two dollar signs? My real table is of course huge which makes manual updating infeasible.

Setting align = TRUE should fix your problem. Stargazer uses then the dcolumn package and generates the following output:
> stargazer(c(-0.2, 1, -2), summary = FALSE, float = FALSE, header = FALSE, align = TRUE)
\begin{tabular}{#{\extracolsep{5pt}} D{.}{.}{-3} D{.}{.}{-3} D{.}{.}{-3} }
\\[-1.8ex]\hline
\hline \\[-1.8ex]
-0.200 & 1 & -2 \\
\hline \\[-1.8ex]
\end{tabular}
If, for whatever reason, you cannot or do not want to use the dcolumn package you can also manually substitute the unwanted signs by:
t <- stargazer(c(-0.2, 1, -2), summary = FALSE, float = FALSE, header = FALSE, align = FALSE)
t <- gsub("\\$\\$\\-\\$", "\\$\\-", t) # substitute unwanted behaviour
write.table(t, row.names = FALSE, col.names = FALSE, quote = FALSE) # recreate latex table
\begin{tabular}{#{\extracolsep{5pt}} ccc}
\\[-1.8ex]\hline
\hline \\[-1.8ex]
$-0.200$ & $1$ & $-2$ \\
\hline \\[-1.8ex]
\end{tabular}

Related

How to put comma after 3 digits in numeric variable in vbscript?

i want to put a comma after 3 digits in a numeric variable in vbscript
w_orimpo = getvalue(rsmodifica , "w_orimpo")
w_orimpo = FormatNumber(w_orimpo,2)
The initial value of w_orimpo is 21960.
If I use FormatNumber I get the value 21,960.
But I would like to get the following one -> 219,60
We can handle this via a regex replacement:
Dim input, output, regex1, regex2
Set input = "21960"
Set regex1 = New RegExp
Set regex2 = New RegExp
regex1.Pattern = "(\d{3})"
regex1.Global = True
regex2.Pattern = ",$"
output = regex1.Replace(StrReverse(input), "$1,")
output = StrReverse(regex2.Replace(output, ""))
Rhino.Print output
Note that two regex replacements are needed here because VBScript's regex engine does not support lookarounds. There is a single regex pattern which would have gotten the job done here:
(\d{3})(?!$)
This would match (and capture) only groups of three digits at a time, and only if those three digits are not followed by the end of the input. This is needed to cover the following edge case:
123456 -> 123,456
We don't want a comma after the final group of three digits. My answer gets around this problem by doing another regex replacement to trim off any trailing comma.
Or without regex:
Mid(CStr(w_orimpo), 1, 3) & "," & Mid(CStr(w_orimpo), 4)
Or
Dim divider
divider = 10 ^ (Len(CStr(w_orimpo)) - 3)
w_orimpo = FormatNumber(w_orimpo / divider, 2)

Variable floating-point precision format string

I'm trying to print floating point numbers as percentages, and I'd like for the number of digits after the decimal place to vary as needed. Currently I have:
fmt.Printf("%.2f%%\n", 100*(value/total))
The problem is that if my percentage is, say, exactly 50, I will get the following output:
50.00%
While what I want is to get:
50%
Is there any way for the format string to indicate that a maximum of 2 digits of precision should be used, but only if needed?
There's no direct solution with the fmt package.
But you can remove the dot and zeros at end with a regular expression:
r, _ := regexp.Compile(`\.?0*$`)
fmt.Printf("%s%%\n", r.ReplaceAllString(fmt.Sprintf("%.2f", 100*(value/total)),""))
Bonus: the same regex works for any number of trailing zeros.
Side note: You'll display 50.0041 the same way than 50, which might be a little misleading.
There's no way to do that inside fmt with e.g. another flag or what have you. You'll have to write out the logic yourself. You could do something like:
var final string
doubledecimal := fmt.Sprintf("%.2f", 100*value/total)
if doubledecimal[len(doubledecimal)-2:] == "00" {
final = doubledecimal[:len(doubledecimal)-3]
} else {
final = doubledecimal
}
fmt.Printf("%s%%\n, final)
You could similarly use strings.Split to split on the decimal point and work from there.
You could even adjust this to turn 50.10% into 50.1%.
doubledecimal := fmt.Sprintf("%.2f", 100*value/total)
// Strip trailing zeroes
for doubledecimal[len(doubledecimal)-1] == 0 {
doubledecimal = doubledecimal[:len(doubledecimal)-1]
}
// Strip the decimal point if it's trailing.
if doubledecimal[len(doubledecimal)-1] == "." {
doubledecimal = doubledecimal[:len(doubledecimal)-1]
}
fmt.Printf("%s%%\n", doubledecimal)
One way could be to have an if statement controlling the print output, i.e. if the result is cleanly divisible by 1 (result%1 == 0) then print the result to no decimal places. Otherwise print to .2f as you've done above. Not sure if there is a shorter way of doing this, but I think this should work.

VBS convert string to floating point

Dim strnumber
strnumber = "0.3"
Dim add
add = 0.1
Dim result
result = strnumber + add
MsgBox result
I want to get 0.4 as result, but get 3.1.
I tried clng(strnumber) and int(strnumber), nothing works. There is a simple solution for sure but I won't find it.
EDIT: Solution
result = CDbl(Replace(strnumber,".",",") + add
Has to do with your locale settings. Automatic conversion (as well as explicit one) observes it in the same manner as in CStr() function.
E.g. in my locale CStr( 0.3) results to 0,3 that is invert to CDbl("0,3") while CDbl("0.3") results to an error.
BTW: always use option explicit and, for debugging purposes, On Error Goto 0
Following below procedures can help:
Replacing the dot(".") with comma (",") in the string
change the string to double by Cdbl
example:
dim a,b,c
a="10.12"
b="5.05"
a=Replace(a,".",",")
b= Replace(b,".",",")
c=Cdbl(a)+Cdbl(b)
msgbox c
You want to add two numbers. So you should use numbers (and not a string (strnumber) and a number (add):
>> n1 = 0.3
>> n2 = 0.1
>> r = n1 + n2
>> WScript.Echo r
>>
0,4
As you can see from the output (0,4), I'm using a locale (German) that uses "," as decimal 'point'. But literals always use ".". So by using the proper data types you can write your scripts in a language/locale independent fashion as long as you don't need to process external string data (from a file or user input). Then you have to modify the input before you feed it to a conversion function like CDbl(). For simple cases that can be done with Replace(inp, badmarker, goodmarker).
P.S. You said you " tried clng(strnumber) and int(strnumber)". You should have tried CDbl(). CLng() tries to get a long integer (cf. CInt()), Int() removes/rounds the fraction from number.

Format number with default NumDigAfterDec and without rounding

I am using in my sql server reports a simple vbscript command to format some numeric fields:
FormatNumber(value,,-1,0,-1)
Important for me is the second parameter NumDigAfterDec that is set to default, meaning
that "the computer's regional settings are used" (http://www.w3schools.com/vbscript/func_formatnumber.asp). This is exactly what I prefer. But when the current number has more digits after decimal, it is rounded. In this case I would like to see all places.
e.g. for two places after decimal, I would like:
0 0.00
90.7 90.70
1.2345 1.2345 (instead of 1.23)
Is this possible without writing code in my reports?
If I understand correctly, you want a condition stating if length of decimal is greater than 2 places, then display full decimal number, else 2 decimal places?
A simple way to do this is:
Dim value : value = "100.54367"
Dim GetDecCount : GetDecCount = Len(Mid(Value, instr(value, ".")+1, len(value)))
if GetDecCount>2 then
msgbox "This number exceeds the regional decimal points of 2, with a length of " & GetDecCount & " decimal points."
else
FormatNumber(value,,-1,0,-1)
end if
Try something like this:
Set re = New RegExp
re.Pattern = ".\d{3,}$"
if re.Test(value) Then
fnum = CStr(value)
Else
fnum = FormatNumber(value, , -1, 0, -1)
End If

ruby regexp to replace equations

I have a some HTML text in mathjax format:
text = "an inline \\( f(x) = \frac{a}{b} \\) equation, a display equation \\[ F = m a \\] \n and another inline \\(y = x\\)"
(Note: equations are delimited by single slashes, e.g. \(, not \\(, the extra \ is just escaping the first one for ruby text).
I want to get the output that substitutes this into, say an image created by latex.codecogs, e.g.
desired_output = "an inline <img src="http://latex.codecogs.com/png.latex?f(x) = \frac{a}{b}\inline"/> equation, a display equation <img src="http://latex.codecogs.com/png.latex?F = m a"/> \n and another inline <img src="http://latex.codecogs.com/png.latex?y = x\inline"/> "
Using Ruby. I try:
desired = text.gsub("(\\[)(.*?)(\\])", "<img src=\"http://latex.codecogs.com/png.latex?\2\" />")
desired = desired.gsub("(\\()(.*?)(\\))", "<img src=\"http://latex.codecogs.com/png.latex?\2\\inline\")
desired
But this is unsuccessful, returning only the original input. What did I miss? How do I construct this query appropriately?
Try:
desired = text.gsub(/\\\[\s*(.*?)\s*\\\]/, "<img src=\"http://latex.codecogs.com/png.latex?\\1\"/>")
desired = desired.gsub(/\\\(\s*(.*?)\s*\\\)/, "<img src=\"http://latex.codecogs.com/png.latex?\\1\inline\"/>")
desired
The important changes that had to happen:
The first parameter for gsub should be a regex (as Anthony mentioned)
If the second parameter is a double-quoted string, then the back references have to be like \\2 (instead of just \2) (see the rdoc)
The first parameter was not escaping the \
There were a couple of other minor formatting things (spaces, etc).
Not sure if your regexp is right - but in Ruby, Regexp are delimited by //, try like this :
desired = text.gsub(/(\\[)(.*?)(\\])/, "<img src=\"http://latex.codecogs.com/png.latex?\2\" />")
You were trying to do string substition, and of course gsub wasn't finding a string containing (\\[)(.*?)(\\])

Resources