How to box characters in latex - latex

Maybe there is a package to do this, but I have not been able to find it. If it doesn't exist, I would appreciate ideas as to how to do ithis. Basically, I need something that would box characters. Something like this:
-------
| A| L|
- - - ...
| 2| 3|
-------
Knuth has something like this in TAOCP, when he discusses MIX's word... without the bottom part, though.
So, a character, and some other char or chars below. Any idea? (I'm foreseeing a tumbleweed)...
Thanks.

Sorry, but I have too little rep to leave a comment. So what you need is a box around each letter without disturbing the text flow? What about a
\framebox{x}?
I don't know enough about latex, but you should look into building your own Latex makro where this is done for each letter that you pass to your new command. Admittedly this is kind of brute force...

You can use \raisebox to fix the vertical height. See the example at the end of this discussion of boxes. Since you can put boxes inside boxes, I think something like this is the answer (gives me a something that looks like your example).
Some text.
\framebox[1.1\width][s]{
\parbox[b]{7.2ex}{
\raisebox{2.0ex}{
\framebox[1.6\width][s]{A}
\framebox[1.6\width][s]{L}
}
\raisebox{0ex}{
\framebox[1.6\width][s]{2}
\framebox[1.6\width][s]{3}
}
}
}
Some more text.

Here's a basic loop that boxes each character (actually each token, so it won't work if there are macros within that take arguments):
\documentclass{article}
\makeatletter
\newcommand\eachboxed[1]{%
\#tfor\#ii:=#1\do{%
\fbox{\strut\#ii}%
}%
}
\makeatother
\begin{document}
\eachboxed{hello}
\end{document}
Not sure if this addresses your problem, however.

If tabular is not what you want, and I think crunchdog is right and it is what you want, perhaps fbox is. Both are well described in the usual sources of LaTeX information.

My instinct is to go with \framebox for single instances or small groups, and to use the tabular environment for more complicated situation (which would usually mean setting up a table).

Related

Getting align in Latex to work for Phonological Rules

First time questioner and extremely new LaTeX user here. I'm trying to get equations to line up according to the arrow symbol used in my linear rules for a Phonology paper.
\begin{align}
\text{/b'al/} &\arrow\ \text{[b\super{j}\textipa{A}l]}\\
\text{/luna/} &\arrow\ \text{[wu.n\textipa{A}]}\\
\end{align}
For this I made \newcommand{\arrow}[0]{$\rightarrow$} to avoivd swapping in and out of mathmode.The format I'm trying to get is that it would have the first and second lines match up according to the arrow. However, I keep getting a "Missing } inserted. } l.38 \end{align}".
Sorry for the possibly dumb question, but can anyone help?
If you are using \arrow in both math and text mode, then you can use
\newcommand{\arrow}{\ensuremath{\rightarrow}}
which will ensure it be typeset in math-mode, regardless of the context. Note that there are some problems with using \ensuremath. However, they may not pertain to your situation.
It looks like the problem is that the align environment already defines math mode, and because your \arrow definition includes the math mode delimiters it breaks the whole thing. You just have to write \rightarrow without the $ ... $ around it.
If you want, define another command \marrow[0]{\rightarrow} to use within math mode.
If this answered your question, please mark it as such so that others know that it worked for you.

Correct way to define macros \etc \ie in latex

In
this article the author discusses the use of \# to put correct spacings after full stops that are not at the end of a sentence e.g. Mr. i.e. etc.
The macro suggested
\newcommand\etc{etc\#ifnextchar.{}{.\#}}
is not quite perfect since in the case (\etc more text) it produces (etc.more text).
I have seen a lot of authors who have made their own versions of the \etc macro, mostly variations on etc.\.
What macros for \etc, \ie, \etal, \eg produce the nicest results in the most situations?
Is this something too personal in taste to be solved in general?
Earlier I used macros for "et al.", etc., but nowadays I would discourage people from defining that kind of macros.
One problem is what you already observed: it's surprisingly tricky to get the definitions right so that they handle all special cases correctly (including the interactions with other packages – e.g., those that re-define the "\cite" command and tweak spacing before references).
But more importantly, even if you have a bunch of macros that suit your needs and you know how to use them, your co-authors are likely to be confused with exactly how to use your macros correctly in various special cases.
Hence I'd recommend that you avoid macros for trivial things such as "et al." and simply spell out everything by using standard Latex markup. After all, most cases don't need any special handling ("e.g." is often followed by a comma; "et al." is often followed by "~\cite", etc.), and whenever special handling is needed, all Latex users should know how to use commands such as "\ " and "\#".
In CVPR's style package, it is defined as:
\usepackage{xspace}
% Add a period to the end of an abbreviation unless there's one
% already, then \xspace.
\makeatletter
\DeclareRobustCommand\onedot{\futurelet\#let#token\#onedot}
\def\#onedot{\ifx\#let#token.\else.\null\fi\xspace}
\def\eg{\emph{e.g}\onedot} \def\Eg{\emph{E.g}\onedot}
\def\ie{\emph{i.e}\onedot} \def\Ie{\emph{I.e}\onedot}
\def\cf{\emph{c.f}\onedot} \def\Cf{\emph{C.f}\onedot}
\def\etc{\emph{etc}\onedot} \def\vs{\emph{vs}\onedot}
\def\wrt{w.r.t\onedot} \def\dof{d.o.f\onedot}
\def\etal{\emph{et al}\onedot}
\makeatother
Have you tried using the xspace package?
Example macro:
\def\etc{etc.\#\xspace}
Some tests:
Cat, dog, \etc. And so on. \\
Cat, dog, \etc! And so on. \\
Cat, dog, \etc, and so on. \\
Cat (dog, \etc). And so on. \\
Produces:
From the documentation:
The xspace package provides a single
command that looks at what comes after
it in the command stream, and decides
whether to insert a space to replace
one "eaten" by the TeX command
decoder.
A technical challenge! We can avoid the problem of letters after spaces by looking at the catcode of the next character and seeing whether or not it is a letter; this can be done with the Latex3's expl3 macro \peek_charcode:NTF (my first expl3 code!):
\documentclass{article}
\usepackage{expl3}
\ExplSyntaxOn
\newcommand\latinabbrev[1]{
\peek_meaning:NTF . {% Same as \#ifnextchar
#1\#}%
{ \peek_catcode:NTF a {% Check whether next char has same catcode as \'a, i.e., is a letter
#1.\# }%
{#1.\#}}}
\ExplSyntaxOff
%Omit final dot from each def.
\def\eg{\latinabbrev{e.g}}
\def\etal{\latinabbrev{et al}}
\def\etc{\latinabbrev{etc}}
\def\ie{\latinabbrev{i.e}}
\begin{document}
Maybe a list, \eg, a, b, c, and d. Which is to say (\ie) a, b, \etc. Consider Knuth, \cf The TeXbook.
\end{document}
Jukka's advice I think is sound, though: I'd say the problem Will works around with his \etc macro we should see as a bug in Tex's implementation of double spacing (Will Robertson should ask for his cheque): if you know the bug is there, you can workaround it directly by putting in \# in cases such as ".)", or you can have tricky code that means you don't have to think in this case, but you have added complexity to the way you typeset which is not going to work for you with the next unexpected glitch, one you probably have introduced yourself.
Postscript Previous version fixed, thanks to Joseph Wright noticing a stupid error at tex.stackexchange.com.
All LaTeX commands eliminate space after them. If you want a space, you need to escape it:
\etc\ and more
This is necessary, because you need to be clear where the command name ends. \etcno space cannot be correctly interpreted.

Latex multicols. Can I group content so it won't split over cols and/or suggest colbreaks?

I'm trying to learn LaTeX. I've been googling this one for a couple days, but I don't speak enough LaTeX to be able to search for it effectively and what documentation I have found is either too simple or goes way over my head (http://www.uoregon.edu/~dspivak/files/multicol.pdf)
I have a document using the multicol package. (I'm actually using multicols* so that the first col fills before the second begins instead of trying to balance them, but I don't think that's relevant here.) The columns output nicely, but I want to be able to indicate that some content won't be broken up into different columns.
For instance,
aaaaaaaa bbbbbbb
aaaaaaaa bbbbbbb
aaaaaaaa
ccccccc
bbbbbbbb ccccccc
That poor attempt at ascii art columns is what's happening. I'd like to indicate that the b block is a whole unit that shouldn't be broken up into different columns. Since it doesn't fit under the a block, the entirety of the b block should be moved to the second column.
Should b be wrapped in something? Is there a block/float/section/box/minipage/paragraph structure I can use? Something specific to multicol? Alternatively is there a way that I can suggest a columnbreak? I'm thinking of something like \- that suggests a hyphenated line break if its convenient, but this would go between blocks.
Thanks!
Would putting the text inside a minipage not work for this?
\begin{minipage}{\columnwidth}
text etc
\end{minipage}
Forcing a column break is as easy as \columnbreak.
There are some gentler possibilities here.
If you decide to fight LaTeX algorithms to the bitter end, there is also this page on preventing page breaks. You can try the \samepage command, but as the page says, "it turns out to be surprisingly tricky".

Punctuation in LaTeX formulas

It seems to be common practice, when writing mathematics, to add punctuation to displayed formulas.
Is there any trick to avoid putting the punctuation mark inside the formula?
I want to avoid
Consider the function
\[ \sin(x).\]
I'd rather have something like:
Consider the function
\[ \sin(x)\].
But of course the full stop is displayed below the formula.
Is there a clever way to separate formulas and punctuation in LaTeX?
\catcode`\#=11
\let\seveendformula\]
\def\]{\#ifnextchar.\PointAndEndFormula\seveendformula}
\def \PointAndEndFormula #1{.\seveendformula}
\catcode`\#=12
Add
More complex solution works with .,?!;: :
\catcode`\#=11
\def\addtopunct#1{\expandafter\let\csname punct#\meaning#1\endcsname\let}
\addtopunct{.} \addtopunct{,} \addtopunct{?}
\addtopunct{!} \addtopunct{;} \addtopunct{:}
\let\seveendformula\]
\def\PunctAndEndFormula #1{#1\seveendformula}
\def\]{\futurelet\punctlet\checkpunct#i}
\def\checkpunct#i{\expandafter\ifx\csname punct#\meaning\punctlet\endcsname\let
\expandafter\PunctAndEndFormula
\else \expandafter\seveendformula\fi}
\catcode`\#=12
There's also the issue of which font the punctuation should be in. You won't see a problem until you try a different math font such as Euler. Then commas and periods are clearly different in text mode and in math mode. I've written text-mode punctuation in displayed formulas as \mbox{,} or lazily as \mbox, just before $$.
You can load the breqn package (compatible with amsmath) and have this behaviour inbuilt with its dmath environment:
Consider the function
\begin{dmath}\sin(x)\end{dmath}.
Will output the same as \[\sin(x).\], i.e., as if the dot was inside the expression.
As a bonus, you'll also get automatic line-breaking in your math equation.
Putting the punctuation inside a display environment is the usual way. The problem is that when Latex processes the \], it ends the mathbox, so anything following will be part of a new vertical box.
You could try something like:
\hbox{\[My formula\]}.
This is not tested, and probably has spacing issues, but if you are interested in this kind of solution, I could try and get something working.
FWIW, you might be interested in https://mathoverflow.net/questions/6675/periods-and-commas-in-mathematical-writing

Replace a character with a string in LaTeX

I'm looking for a way to do a substring replace on a string in LaTeX. What I'd like to do is build a command that I can call like this:
\replace{File,New}
and that would generate something like
\textbf{File}$\rightarrow$\textbf{New}
This is a simple example, but I'd like to be able to put formatting/structure in a single command rather than everywhere in the document. I know that I could build several commands that take increasing numbers of parameters, but I'm hoping that there is an easier way.
Edit for clarification
I'm looking for an equivalent of
string.replace(",", "$\rightarrow$)
something that can take an arbitrary string, and replace a substring with another substring.
So I could call the command with \replace{File}, \replace{File,New}, \replace{File,Options,User}, etc., wrap the words with bold formatting, and replace any commas with the right arrow command. Even if the "wrapping with bold" bit is too difficult (as I think it might be), just the replace part would be helpful.
The general case is rather more tricky (when you're not using commas as separators), but the example you gave can be coded without too much trouble with some knowledge of the LaTeX internals.
\documentclass[12pt]{article}
\makeatletter
\newcommand\formatnice[1]{%
\let\#formatsep\#formatsepinit
\#for\#ii:=#1\do{%
\#formatsep
\formatentry{\#ii}%
}%
}
\def\#formatsepinit{\let\#formatsep\formatsep}
\makeatother
\newcommand\formatsep{,}
\newcommand\formatentry[1]{#1}
\begin{document}
\formatnice{abc,def}
\renewcommand\formatsep{\,$\rightarrow$\,}
\renewcommand\formatentry[1]{\textbf{#1}}
\formatnice{abc,def}
\end{document}
it looks like your "spaces" problem is from a bug in that package. If you surround the "\GetTokens" macro with, say, commas, then you'll see that the extra space is inserted by that macro.
Yes there are bugs in tokenizer package. As I said on my blog, the bugfix is to use the following correcting code instead of just "\usepackage[trim]{tokenizer}":
\usepackage[trim]{tokenizer}
\def\SH#GetTokens#1,#2\#empty{%
\def\SH#token{#1}%
\ifx\SH#trimtokens\SH#true% strip spaces if requested
\TrimSpaces\SH#token%
\fi%
\SH#DefineCommand{\SH#FirstArgName}{\SH#token}%
\SH#DefineCommand{\SH#SecondArgName}{#2}%
}
\def\SH#CheckTokenSep#1,#2\#empty{%
\def\SH#CTSArgTwo{#2}%
\ifx\SH#CTSArgTwo\#empty%
\edef\SH#TokenValid{\SH#false}%
\else%
\edef\SH#TokenValid{\SH#true}%
\fi%
}
I will report this bugfix to the developer Sascha Herpers
Try the xstring package:
\usepackage{xstring}
[…]
\StrSubstitute{File,New}{,}{\(\rightarrow\)}
There's a LaTeX package called tokenizer which may help you to do what you want.
Here's a hack (but pure LaTeX, no internals) which gets close to what I think you want, but with some extraneous spaces I haven't quite been able to fix. Perhaps Will Robertson can advise further? Unlike his slightly more polished answer, I haven't parameterised the bits and pieces, Here goes:
\usepackage{forloop}
\usepackage[trim]{tokenizer}
...
\newcounter{rrCount}
\newcommand{\replace}[1]{%
\GetTokens{rrFirst}{rrRest}{#1,}%
\textbf{\rrFirst}%
\forloop{rrCount}{0}{\value{rrCount} < 100}{%
\ifthenelse{\equal{\rrRest}{}}{%
\setcounter{rrCount}{101}%
}{%
\GetTokens{rrFirst}{rrRest}{\rrRest}%
$\rightarrow$\textbf{\rrFirst}%
}%
}%
}%
% -----------------------------------------------------------------
\replace{a1}\\
\replace{a2,b2}\\
\replace{a3,b3,c3}\\
OK, I withdraw this answer. Thanks for clarifying the question.
I suspect this may not be what you want, but here goes anyway:
\newcommand{\replace}[2]{\textbf{#1}$\rightarrow$\textbf{#2}}
\replace{File}{New}
If this isn't what you're looking for, could you clarify the question, please?

Resources