How to export a org-mode table to LaTeX with the correct placement [h]? - emacs

I'm trying to export a table to LaTeX form my org-file which looks like this:
#+LATEX_HEADER: \usepackage{adjustbox}
* table test
#+begin_table
#+LATEX: \caption{my caption}
label:tab:mylabel
#+ATTR_LATEX: :placement [h]
#+LATEX: \centering
#+LATEX: \adjustbox{max width=\linewidth}{
#+ATTR_LATEX: :center nil
| | |
| id | Question |
|----+--------------------------|
| | |
| 1 | Does it export with [h]? |
#+end_table
I want to get the exported TeX to look like this:
...
\begin{table}[h]
\caption{my caption}
\label{tab:mylabel}
\centering
\adjustbox{max width=\linewidth}{
\begin{tabular}{ll}
& \\
id & Question\\
\hline
& \\
1 & Does it export with [h]?\\
\end{tabular}
\end{table}
...
but I'm just getting
...
\begin{table}
\caption{my caption}
\label{tab:mylabel}
\centering
\adjustbox{max width=\linewidth}{
\begin{tabular}{ll}
& \\
id & Question\\
\hline
& \\
1 & Does it export with [h]?\\
\end{tabular}
\end{table}
...
using the following versions
GNU Emacs 25.1.1 (x86_64-apple-darwin15.5.0, NS appkit-1404.47 Version 10.11.5 (Build 15F34)) of 2017-01-06
Org mode version 9.0.5 (release_9.0.5-444-g998576 # ~/git/org-mode/lisp/)
I also tried
#+ATTR_LATEX: :float t :placement [h]
#+ATTR_LATEX: :center nil :float t :placement [h]
#+ATTR_LATEX: :center nil :placement [h]
#+begin_table :placement [h]
#+begin_table :float t :placement [h]
My search on the internet only found some emails from 2010, when org-latex.el existed. Since it still exists in the documentation I thought it should work somehow.
So can anyone help me? Or leak the magic words I have to feed Google with to find my answer?
Thx #Nick, we're on the right way but sadly not at the end of it.
Well I did not want to break the parsing, it explains some things.
I've got some more requirements and things to explain.
At First some of my tables are a bit wider like this:
| id | Question |
|----+---------------------------------------------------------------------------------------------------------------------------|
| 1 | Does it export with [h]? |
| 2 | I have some tables witch are very wide so What if you have a really wide table wich needs to be shrinked to the right size |
The Adjustbox package is the first I found that shrinks the whole table to the correct width. I tried your suggested tabularx but it's not shrinking the contents of the table. Without the shrinking most of my tables look weird.
The second Thing, I'm using org-ref. With your version (which I would prefer when the 2 probs are gone) exports to:
\caption{\label{tab:org56e3a68}
My caption}
I added #+label: tab:my-label as intended in the documentation. Added it before and after #+caption: and #+name: but it does not change anything in the .tex file.
any idea?

I can do everything you want, except the adjustbox stuff with this:
#+BIND: org-latex-prefer-user-labels t
* table test
#+caption: My caption
#+name:my-label
#+ATTR_LATEX: :placement [h] :center t
| | |
| id | Question |
|----+--------------------------|
| | |
| 1 | Does it export with [h]? |
I don't know what \adjustbox does or how it has to be used and where it has to be placed; however, you can't add #+LATEX: lines in between the #+caption and the table: they apparently break the parsing.
You could add another attribute to the ATTR_LATEX line:
#+ATTR_LATEX: :placement [h] :center t :width \linewidth
but it's not clear that does what you want. Also, I just tested and it does not do anything: that's because the standard environment is tabular which does not take a width argument. The following works:
#+ATTR_LATEX: :environment tabularx :placement [h] :center t :width \linewidth
but you also have to add
#+LATEX_HEADER: \usepackage{tabularx}
at the top of the file.

Related

Creating macros in netbeans

I wish to create macro in Netbeans to put block comment over function. I have preference of code formatting over file save. So When I close file it saves code automatically and format it.
Issue is when I create function and comment it. It unformatted my whole block of code like this.
/**
*function abc(){
*var a, b = 50;
*}
*/
I wish to create comment like this. so it keep my coding properly formatted as well.
/*
|
| function abc(){
| var a, b = 50;
| }
|
*/
You can add your own macro by Following this instructions:
Edit->Start Macro Recording
Edit->Stop Macro Recirding
It Will pop-up one Box For Editor Macros->Add Your Choice of Macro Name
In code area add Your custom comment code in " your code "; Like,
Blockquote
"/*
|
| function abc(){
| var a, b = 50;
| }
|
*/"
Assign a Short Cut Key to your Custom Macro.
That's It
Though I couldn't find macro for the same. But I found alternative. Use Ctrl+Shift+R toggle, for multiple line action at same time & add pipeline sign. But it take extra effort for starting and ending comment.

Produce table with text and number with fprintf in Matlab

I need to produce a table whose first 2 columns have text, and the remaining 2 have numbers. Something like this:
| Ford | Mustang | 1975 | 35 |
| Chev | Camaro | 1976 | 38 |
I have the string in a cell, and the numeric variables in a matrix. I've tried with fprintf but can't make it work. I have no problems doing it in xlswrite, but I don't want to go that way. Any ideas please?
Thanks!
You could use fprintf in a loop like this:
fprintf(1, '| %8s | %8s | %4d | %2d |\n', ...
company{i}, model{i}, year(i), otherNumber(i));
to write to stdout. You can also modify the %#s if you want different spacing in your table, or provide a different file descriptor to the first argument.

How to increment a serie of number on the same row in a configuration file?

Let say I have a configuration file, and each lines contains space separated values. On on the column I have only zeros. Example:
... # there is more configuration before the interesting stuff:
0 0 file /dev/stdin 224.0.68.54:12131
0 0 file /dev/stdin 224.0.68.55:12102
0 0 file /dev/stdin 224.0.68.49:12333
0 0 file /dev/stdin 224.0.68.60:12184
0 0 file /dev/stdin 224.0.68.62:12888
0 0 file /dev/stdin 224.0.68.77:12001
0 0 file /dev/stdin 224.0.68.33:12973
Now I want to increment the second column with its index.
that is I want this result:
0 0 file /dev/stdin 224.0.68.54:12131
0 1 file /dev/stdin 224.0.68.55:12102
0 2 file /dev/stdin 224.0.68.49:12333
0 3 file /dev/stdin 224.0.68.60:12184
0 4 file /dev/stdin 224.0.68.62:12888
0 5 file /dev/stdin 224.0.68.77:12001
0 6 file /dev/stdin 224.0.68.33:12973
How to to that in emacs lisp? Or any other Emacsen way of doing thing please?
You can use the possibility to search and replace by an evaluated expression:
put the point on the first line to process
M-x query-replace-regexp RET
searched string: ^0 \([0-9]+\)
replace with: \,(format "0 %s" (+ \#1 \#))
The meaning is:
search a number preceded by a single zero at the beginning of the line
replace by the result of the evaluation where \#1 is the first matched group (like \1 but converted to number) and \# is the number of replacements already done (begins at 0 for the first replacement). The expression is evaluated for each match.
If the first number is not always a zero, I would use:
searched string: ^\([0-9]+\) \([0-9]+\)
replace with: \,(format "%s %s" \#1 (+ \#2 \#))
You can use a macro with a counter to do that. You start defining a macro with F3 and end the definition with F4. While defining the macro, hitting F3 again will insert the value of the counter and increment it. After defining the macro, run it by hitting F4.
So in your case, move point to the beginning of the first interesting line, hit F3 C-f C-f C-d F3 C-n C-a F4 (i.e. remove the second zero, insert the counter, and move to the beginning of the next line). Then hit F4 as many times as needed to change all the lines.
If you need to change the value of the counter, use M-x kmacro-set-counter.
Similarly to Jonathan Leech-Pepin's answer, you can also use the non-cua rectangle editing commands (albeit in two steps instead of one).
Set mark and point to the corners of the column rectangle
C-xrk to delete the rectangle.
C-uC-xrN to insert a number sequence in its place.
Also see related question:
emacs string-insert-rectangle vector of numbers?
Something you could do using org-mode (I'm not sure of how will you use the result, but this can be one way of doing it)
| N | X | file | stream | ip |
|---+---+------+------------+-------------------|
| 0 | 0 | file | /dev/stdin | 224.0.68.54:12131 |
| 0 | 1 | file | /dev/stdin | 224.0.68.55:12102 |
| 0 | 2 | file | /dev/stdin | 224.0.68.49:12333 |
| 0 | 3 | file | /dev/stdin | 224.0.68.60:12184 |
| 0 | 4 | file | /dev/stdin | 224.0.68.62:12888 |
| 0 | 5 | file | /dev/stdin | 224.0.68.77:12001 |
| 0 | 6 | file | /dev/stdin | 224.0.68.33:12973 |
#+TBLFM: $2=##-2
Some explanation, or rather, how to get your original data and transform it into this table:
Copy your data into file config.org
Create the table by basic string replacement commands. (Just copy the text as you have it now, select all of it and then C-c |)
The #+TBLFM: part is the "formula" for this table, it means the following: assign to every cell in the second column the ordinal of it's row minus 2 (they start counting rows at 1, and this table has a header row).
Whenever you C-c C-c on the formula, it will re-apply it to the table (so you can append more rows as you modify the document, reapplying the formula will automatically update the table.)
You can use cua-set-rectangle-mark.
Evaluate:
(cua-mode 't)
Then in the buffer you can use M-x cua-set-rectangle-mark at the second column then extend the rectangle down to the end of the list.
Then use M-n (sequence), the defaults of Start (0):, Increment (1): and Format (%d): all provide what you need in this case.
Here a very easy way to do this (using the same technique as Seki's answer but easier):
Select all the lines you want the increment to appear in, then issue
M-x replace-regexp "^0 0" "0 \#"
or in more detail:
M-x replace-regexp RET
Searched string: ^0 0
Replace with: 0 \#
This will give you the desired result, because \# replaces to the number of replacements done.
I run into this problem all the time, and I don't enjoy setting up complicated rectangles or macros. So I created an elisp function to do it for me in a single command. I call it...*drumroll*...the downcrementer
Placing your cursor on the second 0 in your example and running M-x downcrementer will sequentially increment all the numbers in the column in one shot.
(defun downcrementer--bounds-of-num-at-point ()
(interactive)
(let (num-start num-end)
(save-excursion
(skip-chars-backward "0-9")
(setq num-start (point)))
(save-excursion
(skip-chars-forward "0-9")
(setq num-end (point)))
(list num-start num-end)))
(defun downcrementer (&optional prefix-arg)
(interactive "P")
(let ((current-number (string-to-number
(apply #'buffer-substring-no-properties
(downcrementer--bounds-of-num-at-point))))
(stride 1)
(orig-truncate-lines truncate-lines))
(when prefix-arg
(setq stride (string-to-number (read-string "Enter stride: "))))
;; Prevent wrapping from causing next-line to go to the wrong place
(toggle-truncate-lines t)
(next-line)
(let ((first-column (current-column)))
(while (thing-at-point 'number t)
(save-excursion
(apply #'delete-region (downcrementer--bounds-of-num-at-point))
(setq current-number (+ current-number stride))
(insert (number-to-string current-number)))
(next-line)
(move-to-column first-column))
(previous-line)
(move-to-column first-column))
;; Restore original truncate-line setting
(toggle-truncate-lines orig-truncate-lines)))

xText Variable/Attribute Assignment

I built a grammar in xText to recognize formal expressions of a specific format
and to use the generated object tree in Java.
This is what it looks like:
grammar eu.gemtec.device.espa.texpr.Texpr with org.eclipse.xtext.common.Terminals
generate texpr "http://www.gemtec.eu/device/espa/texpr/Texpr"
Model:
(expressions+=AbstractExpression)*
;
AbstractExpression:
MatcherExpression | Assignment;
MatcherExpression:
TerminalMatcher ({Operation.left=current} operator='or' right= MatcherExpression)?
;
TerminalMatcher returns MatcherExpression:
'(' MatcherExpression ')' | {MatcherLiteral} value=Literal
;
Literal:
CharMatcher | ExactMatcher
;
CharMatcher:
type=('text'|'number'|'symbol'|'whitespace') ('(' cardinality=Cardinality ')')?
;
/* Kardinalitäten für CharMatcher*/
Cardinality:
CardinalityMin | CardinalityMinMax | CardinalityMax| CardinalityExact
;
CardinalityMin: min=INT '->';
CardinalityMinMax: min=INT '->' max=INT;
CardinalityMax: '->' max=INT;
CardinalityExact: exact=INT;
ExactMatcher:
(ignoreCase='ignoreCase''(' expected=STRING ')') | expected=STRING
;
/* Variablenzuweisung
*
* z.B. $myVar=number
* */
Assignment:
'$' name=ID '=' expression=MatcherExpression
;
Everything works fine except for the 'cardinality' assignment.
The Expressions look like this:
text number(3) - (an arbitrary amount of letters followed by exactly 3 numbers)
symbol number(2->) - (an arbitrary amount of special characters followed by at least 2 numbers)
whitespace number(->4) - (an arbitrary amount of whitespaces followed by a maximum of 4 numbers)
number(3->6) - (at least 3 numbers but not more than 6)
When I run Eclipse with this grammar (so that my language is recognized and has code completion and so on), everything I type is shown in the "Outline"-tab as a tree-structure as it should, except for the cardinality values.
When I add a cardinality statement to a CharMatcher, the little plus appears before it, but when I click on it it just disappears.
Can anyone tell me why this does not work?
I found the solution myself, I think the problem was that the compiler could not decide which class to create at this point:
Cardinality:
CardinalityMin | CardinalityMinMax | CardinalityMax| CardinalityExact
;
CardinalityMin: min=INT '->';
CardinalityMinMax: min=INT '->' max=INT;
CardinalityMax: '->' max=INT;
CardinalityExact: exact=INT;
So I simplified the whole thing a little, it now looks like this:
Cardinality:
CardinalityMinMax | CardinalityExact
;
CardinalityMinMax: (min=INT '..' max=INT) | (min=INT '..') | ('..' max=INT);
CardinalityExact: exact=INT;
It is still not shown in the "Outline"-Tab, but I suppose that is a problem of the visualisation.
The generated classes now work as intended.

when to quote symbol in Emacs Lisp

I've beginning learning programming with Emacs Lisp. I'm so confused by symbol quotation.
For example:
(progn
(setq a '(1 2))
(prin1 a)
(add-to-list 'a 3)
(prin1 a)
(setcar a 4)
(prin1 a)
(push 5 a)
""
)
why the "add-to-list" function need a quoted symbol as its first argument, while the "setcar" and "push" function need no argument quotation?
Here's a diagram that represents the symbol a and its value after (setq a '(1 2)). The boxes are elementary data structures (symbols and conses) and the arrows are pointers (where a piece of data references another). (I'm simplifying a little.)
symbol cons cons
+-------+----------+ +------+------+ +------+------+
|name: |variable: | |car: |cdr: | |car: |cdr: |
| a | | | | 1 | | | | 2 | nil |
+-------+----|-----+ +------+--|---+ +------+------+
| ​↑ | ↑
+-------------+ +-------+
The expression '(1 2) builds the two conses on the right, which make up a two-element list. The expression (setq a '(1 2)) creates the symbol a if it doesn't exist, then makes its “variable slot” (the part that contains the value of the symbol) point to the newly created list. setq is a built-in macro, and (setq a '(1 2)) is shorthand for (set 'a '(1 2)). The first argument of set is the symbol to modify and the second argument is the value to set the symbol's variable slot to.
(add-to-list 'a 3) is equivalent to (set 'a (cons 3 a)) here, because 3 is not in the list. This expression does four things:
Create a new cons cell.
Set the new cons cell's car field to 3.
Set the new cons cell's cdr field to the former (and still current) value of a (i.e. copy the contents of a's variable slot).
Set the variable slot of a to the new cons cell.
After that call, the data structures involved look like this:
symbol cons cons cons
+-------+----------+ +------+--|---+ +------+------+ +------+------+
|name: |variable: | |car: |cdr: | |car: |cdr: | |car: |cdr: |
| a | | | | 3 | | | | 1 | | | | 2 | nil |
+-------+----|-----+ +------+--|---+ +------+--|---+ +------+------+
| ​↑ | ↑ | ↑
+-------------+ +-------+ +-------+
The call to setcar doesn't create any new data structure, and doesn't act on the symbol a but on its value, which is the cons cell whose car currently contains 3. After (setcar a 4), the data structures look like this:
symbol cons cons cons
+-------+----------+ +------+--|---+ +------+------+ +------+------+
|name: |variable: | |car: |cdr: | |car: |cdr: | |car: |cdr: |
| a | | | | 4 | | | | 1 | | | | 2 | nil |
+-------+----|-----+ +------+--|---+ +------+--|---+ +------+------+
| ​↑ | ↑ | ↑
+-------------+ +-------+ +-------+
push is a macro; here, (push 5 a) is equivalent to (set 'a (cons 5 a)).
setq and push are macros (setq is a “special form”, which as far as we're concerned here means a macro whose definition is built into the interpreter and not provided in Lisp). Macros receive their arguments unevaluated and can choose to expand them or not. set, setcar and add-to-list are functions, which receive their arguments evaluated. Evaluating a symbol returns the contents of its variable slot, e.g. after the initial (setq a '(1 2)) the value of the symbol a is the cons cell whose car contains 1.
If you're still confused, I suggest experimenting with (setq b a) and seeing for yourself which of the expressions modify b when you act on a (the ones that act on the symbol a) and which don't (the ones that act on the value of the symbol a).
Functions evaluate their arguments before execution, so quote when you need to pass an actual symbol (as pointer to some data structure, for example) and don't quote when it's a variable value.
add-to-list performs in-place mutation of its first argument, so it needs a quoted symbol.
push is not a function, but a macro; that is why it's able to accept unquoted arguments without evaluation. Builtin forms, like setcar, also do not have that limitation.
The other answers given so far clarify the use of quote and the difference between functions, on the one hand, and macros and special forms on the other hand.
However, they do not get to another part of the question: why is add-to-list as it is? Why does it require its first argument to be a symbol? That's a separate question from whether or not it evaluates the argument. It's the real question behind the design of add-to-list.
One could imagine that add-to-list evaluated its args, and expected the value of the first arg to be a list, and then added the value of the second arg to that list as an element and returned the result (new list or same list). That would let you do (add-to-list foo 'shoe) to add the symbol shoe to the list that is the value of foo -- say (1 2 buckle) --, to give (1 2 buckle shoe).
The point is that such a function would not be very useful. Why? Because the list value isn't necessarily accessible. The variable foo might be thought of as a way to access it -- a "handle" or "pointer" to it. But that is not true of the list that the function returns. That returned list can be composed of new list structure, and there is typically nothing (no variable) pointing to that list. The function add-to-list never sees the symbol (variable) foo -- it has no way of knowing that the list value it receives as first argument is bound to foo. If add-to-list were designed that way then you would still need to assign its returned result to your list variable.
IOW, add-to-list evaluates its args because it is a function, but that doesn't explain much. It expects a symbol as the value of its first arg. And it expects the value of that variable (symbol) to be a list. It adds the value of its second arg to the list (possibly changing the list structure), and it sets the value of the variable that is the value of its first arg to that list.
Bottom line: It needs a symbol as arg because its job is to assign a new value to that symbol (the new value being the same list value or the same value with the new list element added at the front).
And yes, another way to go would be to use a macro or special form, as in push. It's the same idea: push wants a symbol as its second arg. The difference is that push does not evaluate its args so the symbol need not be quoted. But in both cases (in Emacs Lisp) the code needs to get hold of a symbol in order to set its value to the augmented list.