Writing operators in the position basis To calculate an expecation value in quantum mechanics of some operator $\hat A$ you  can generally write it in Dirac notation
$$\langle \hat A\rangle=\langle\psi|\hat A|\psi\rangle.$$
In the position basis this becomes the following integral:
$$\int \mathrm {d}x\ \psi^*(x)A(x)\psi(x).$$
But when I try to derive this explicitly from Dirac notation I get
\begin{align}\langle\psi|\hat A|\psi\rangle
&=\langle\psi|\left(\int \mathrm{d}x|x\rangle\langle x|\right)\hat A\left(\int \mathrm{d}x'|x'\rangle\langle x'|\right)|\psi\rangle\\
&=\int \mathrm{d}x\ \mathrm{d}x'\langle\psi|x\rangle\langle x|\hat A|x'\rangle\langle x|\psi\rangle\\
&=\int \mathrm{d}x\ \mathrm{d}x'\ \psi^*(x)\,\langle x|\hat A|x'\rangle\,\psi(x').
\end{align}
The reason I'm stuck is that I don't remember ever calculating the matrix elements in the position basis. How do you calculate these matrix elements in the position basis (or more generally in any basis)?
 A: In order to calculate the matrix elements of an operator, you need to know how the operator acts on the basis states. This is usually something that you're given (because it's almost always how an operator is defined in the first place - by its action on basis states). So, if you want to calculate the matrix elements, you need at least that much information (which can be provided directly, or in shorthand using the operator's position-space representation described below).
If $\hat{A}$ happens to have every $|x\rangle$ basis state as an eigenstate, then you can write $\hat{A}|x\rangle=A(x)|x\rangle$, where $A(x)$ is the operator's position-basis representation. Then you can say that $\langle x'|\hat{A}|x\rangle=A(x)\langle x'|x\rangle=A(x)\delta(x-x')$. For example, for the simple-harmonic-oscillator potential operator $\hat{A}=\kappa\hat{x}^2$, we have that $\langle x'|\hat{A}|x\rangle=\kappa x^2\delta(x-x')$.
This condition is not necessarily true in general, though, and there are many operators which simply don't have a nice position-space representation like this. My favorite example is the position translation operator $\hat{T}_a$. The action of this operator on the basis states is defined to be as follows: $\hat{T}_a|x\rangle=|x+a\rangle$. As you can see, this operator has none of the position basis states as eigenstates! So the matrix element will look slightly different:
$$\langle x'|\hat{T}_a|x\rangle=\langle x'|x+a\rangle=\delta(x+a-x')$$
which you'll notice doesn't conform to the structure $A(x)\delta(x-x')$.
